题意:
求给你个串,前i位子串由某个字符串重复k次得到,求所有的i和k
分析:
i-next[i]恰好是一个循环节
#include <map> #include <set> #include <list> #include <cmath> #include <queue> #include <stack> #include <cstdio> #include <vector> #include <string> #include <cctype> #include <complex> #include <cassert> #include <utility> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> using namespace std; typedef pair<int,int> PII; typedef long long ll; #define lson l,m,rt<<1 #define pi acos(-1.0) #define rson m+1,r,rt<<11 #define All 1,N,1 #define read freopen("in.txt", "r", stdin) const ll INFll = 0x3f3f3f3f3f3f3f3fLL; const int INF= 0x7ffffff; const int mod = 1000000007; int n,next[1000001]; char s[10000001]; void getNext(){ next[0]=-1; int i=0,j=-1; while(i<n){ if(j==-1||s[i]==s[j]){ i++; j++; next[i]=j; } else j=next[j]; } } int main() { int num=0; while(~scanf("%d",&n)){ if(n==0)break; scanf("%s",s); printf("Test case #%d\n",++num); getNext(); for(int i=1;i<=n;++i){ if(i%(i-next[i])==0&&next[i]!=0) printf("%d %d\n",i,i/(i-next[i])); } printf("\n"); } return 0; }
时间: 2024-10-10 03:29:13