题目:把串补成最小周期>=2的串需要补的长度
规律:把一个串补成R^k串需要补的长度:补上最小周期 - 长度%最小周期
/************************************************ Author :DarkTong Created Time :2016/8/14 22:26:17 File Name :Hdu_3746.cpp *************************************************/ #include <bits/stdc++.h> using namespace std; typedef unsigned long long ULL; typedef long long LL; const int INF = 0x3f3f3f3f; const double eps = 1e-9; const int maxn = 100000 + 10; char s[maxn]; int nxt[maxn], m; void getfail(char *P, int *f) { // int m = strlen(P); f[0]=0; f[1]=0; for(int i=1;i<m;++i) { int j=f[i]; while(j&&P[i]!=P[j])j=f[j]; f[i+1]=P[i]==P[j]?j+1:0; } } int main() { int T, cas=1; scanf("%d", &T); while(T--) { scanf("%s", &s); m = strlen(s); getfail(s, nxt); // for(int i=1;i<=m;++i) cout<<nxt[i]<<" "; cout<<endl; int ans = INF; ans = m%(m-nxt[m]); if(ans==0&&nxt[m]==0) ans=m; else if(ans)ans = (m-nxt[m])-ans; printf("%d\n", ans); } return 0; }
转载于:https://www.cnblogs.com/DarkTong/p/5771335.html