大佬们,本菜鸡先炸栈,然后边界处理错了,然后吧strlen放到了for里面,最终还是32分,救救孩子吧
by gjh303987897 @ 2022-09-05 21:16:10
@[gjh303987897](/user/181715) 改了一下,代码如下:
```cpp
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
int len_a;
const int maxn = 1.1e7+11;
char a[maxn],b[maxn<<1];
int p[maxn<<1];
void add_str(){
int cnt = 2;
b[0]='^';
b[1]='#';
for(int i=0;i<len_a;i++){
b[cnt++]=a[i];
b[cnt++]='#';
}
b[cnt]='@';
}
int main(){
scanf("%s",a);
len_a=strlen(a);
add_str();
int r=0,mid=0;
int ans=0;
int len_b=strlen(b);
for(int i=1;i<=len_b;i++){
if(i>r)
p[i]=1;
else{
p[i]=std::min(p[mid*2-i],r-i+1);
}
while(i - p[i] >= 0 && i + p[i] < len_b && b[i-p[i]]==b[i+p[i]]) ++p[i];
if(p[i]+i-1>r){
r=p[i]+i-1;
mid=i;
}
if(ans<p[i]) ans=p[i];
}
std:: cout<<ans-1;
return 0;
}
```
在主函数for,lenb这个训话中while后边的判断应该是$p_i+i-1$,改完之后可以获得52分,然后再while里特判了一下边界,还有就是$p_i=r-i+1$,不是$p_i=r-mid+1$!
by FiraCode @ 2022-09-08 13:22:28
@[FiraCode](/user/528430) 感谢感谢?,while里要判断边界的原因是因为字符串两边的“#”可能会使答案比原先大吗?
by gjh303987897 @ 2022-09-08 16:09:38
@[gjh303987897](/user/181715) 不是,只不过是判断一下,怕数组越界,当然删掉也可以过。
by FiraCode @ 2022-09-08 19:22:02
@[FiraCode](/user/528430) OKOK,谢谢谢谢
by gjh303987897 @ 2022-09-08 21:03:36
@[gjh303987897](/user/181715) 不用谢!
by FiraCode @ 2022-09-08 21:15:05