标签:blank long ali pac code i++ span tom esc
题意:找每个前缀的最小循环节,如果有就输出位置和循环次数
思路:kmp
#include <cstdio> #include <map> #include <iostream> #include<cstring> #include<bits/stdc++.h> #define ll long long int #define M 6 using namespace std; inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;} int moth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int dir[4][2]={1,0 ,0,1 ,-1,0 ,0,-1}; int dirs[8][2]={1,0 ,0,1 ,-1,0 ,0,-1, -1,-1 ,-1,1 ,1,-1 ,1,1}; const int inf=0x3f3f3f3f; const ll mod=1e9+7; int nextt[1000007]; void getnext(string s){ nextt[1]=0; int len=s.length(); for(int i=2,j=0;i<=len;i++){ while(j>0&&s[i-1]!=s[j]) j=nextt[j]; if(s[i-1]==s[j]) j++; nextt[i]=j; } } int main(){ ios::sync_with_stdio(false); int n; int cnt=0; while(cin>>n&&n){ string s; cin>>s; getnext(s); int len=s.length(); cout<<"Test case #"<<++cnt<<endl; for(int i=2;i<=len;i++){ if(nextt[i]==0) continue; int t=i-nextt[i]; if(i%t==0){ cout<<i<<" "<<i/t<<endl; } } cout<<endl; } return 0; }
标签:blank long ali pac code i++ span tom esc
原文地址:https://www.cnblogs.com/wmj6/p/10423232.html