未理解透,鬼知道怎么A的
蒟蒻交了个乱猜贪心搞了10pts,一翻题解群佬乱舞,最后DP解决
$\exists i - next[i] <= j, f[j] = f[next[i]] $
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long
#define ON_DEBUG
#ifdef ON_DEBUG
#define D_e_Line printf("\n\n----------\n\n")
#define D_e(x) cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin);
#else
#define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ;
#endif
struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std;
const int N = 500007;
char str[N];
int nxt[N];
int f[N], pos[N];
int main(){
//FileOpen();
scanf("%s",str + 1);
int len = strlen(str + 1);
nxt[0] = -1;
int j = 0;
R(i,2,len){
while(j != -1 && str[j + 1] != str[i]) j = nxt[j];
nxt[i] = ++j;
}
f[1] = 1;
j = 0;
R(i,2,len){
f[i] = i;
if(pos[f[nxt[i]]] >= i - nxt[i]) f[i] = f[nxt[i]];
pos[f[i]] = i;
}
printf("%d", f[len]);
return 0;
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章