1、题目:给定一个字符串,问最多是多少个相同子串不重叠连接构成。
KMP的next数组应用。这里主要是如何判断是否有这样的子串,和子串的个数。
若为abababa,显然除其本身外,没有子串满足条件。而分析其next数组,next[7] = 5,next[5] = 3,next[3] = 1,即str[2..7]可由ba子串连接构成,那怎么否定这样的情况呢?
很简单,若该子串满足条件,则len%sublen必为0。sunlen可由上面的分析得到为len-next[len]。因为子串是首尾相接,len/sublen即为substr的个数。
若L%(L-next[L])==0,n = L/(L-next[L]),else n = 1
- #include<iostream>
- #include<cstdio>
- using namespace std;
- char pattern[1000002];
- int next[1000002];
- /*
- kmp算法,需要首先求出模式串的next函数值
- next[j] = k,说明 p0pk-1 == pj-kpj-1,也就是说k为其前面相等串的长度
- */
- void get_nextval(const char* pattern)
- {
- int i=0,j=-1;
- next[0]= -1;
- while(pattern[i] != '\0')
- {
- if(j== -1 || pattern[i]== pattern[j] ) //pattern[i]表示后缀的单个字符,pattern[j]表示前缀的单个字符
- {
- ++i;
- ++j;
- if(pattern[i] != pattern[j])
- next[i]=j;
- else
- next[i]=next[j];
- }
- else
- j=next[j]; //若j值不相同,则j值回溯
- }
- }//get_nextval
- int main(void)
- {
- int len;
- while(scanf("%s",pattern)!=EOF)
- {
- if(pattern[0]=='.')
- break;
- len=strlen(pattern);
- get_nextval(pattern);
- if(len%(len-next[len])==0)
- printf("%d\n",len/(len-next[len]));
- else
- printf("1\n");
- }
- return 0;
- }
2、题目:定义字符串A,若A最多由n个相同字串s连接而成,则A=s^n,
比如"aaa" = "a"^3,"abab" = "ab"^2, "ababa" = "ababa"^1。
分析:KMP。若某一长度L的前缀符合上诉条件,则
1) next[L]!=0(next[L]=0时字串为原串,不符合条件)
2) L%(L-next[L])==0(此时字串的长度为L/next[L])
综上,若L%S==0,则可得L为str[0]...str[s-1]的相同字串组成,总长度为L,其中字串长度SL = s-0+1=L-next[L],循环次数为L/SL。故对于所有大于1的前缀,只要其符合上述条件,即为答案之一。
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- using namespace std;
- char pattern[1000002];
- int next[1000002];
- /*
- kmp算法,需要首先求出模式串的next函数值
- next[j] = k,说明 p0pk-1 == pj-kpj-1,也就是说k为其前面相等串的长度
- */
- void get_nextval(const char* pattern)
- {
- int i=0,j=-1;
- next[0]= -1;
- while(pattern[i] != '\0')
- {
- if(j== -1 || pattern[i]== pattern[j] )
- {
- ++i;
- ++j;
- next[i]=j;
- }
- else
- j=next[j];
- }
- }//get_nextval
- int main(void)
- {
- int i,len,n,j=1;
- while(scanf("%d",&n)!=EOF)
- {
- if(!n)
- break;
- scanf("%s",pattern);
- len=strlen(pattern);
- get_nextval(pattern);
- printf("Test case #%d\n",j++);
- for(i=2;i<=len;i++)
- {
- if(i%(i-next[i])==0 && i/(i-next[i])>1)
- printf("%d %d\n",i,i/(i-next[i]));
- }
- printf("\n");
- }
- return 0;
- }
3、题目:给出一个字符串A,求A有多少个前缀同时也是后缀,从小到大输出这些前缀的长度。
分析:对于长度为len的字符串,由next的定义知:
A[0]A[1]...A[next[len]-1]=A[len-next[len]]...A[len-1]此时A[0]A[1]...A[next[len]-1]为一个符合条件的前缀。
有A[0]A[1]....A[next[next[len]]-1] = A[len-next[next[len] - next[next[len]]]...A[next[len]-1],故A[0]A[1]....A[next[next[len]]-1]也是一个符合条件的前缀。
故从len=>next[len]=>next[next[len]] ....=>直到某个next[]为0均为合法答案,注意当首位单词相同时,也为答案。
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<vector>
- using namespace std;
- char pattern[400002];
- int next[400002];
- /*
- kmp算法,需要首先求出模式串的next函数值
- next[j] = k,说明 p0pk-1 == pj-kpj-1,也就是说k为其前面相等串的长度
- */
- void get_nextval(const char* pattern)
- {
- int i=0,j=-1;
- next[0]= -1;
- while(pattern[i] != '\0')
- {
- if(j== -1 || pattern[i]== pattern[j] )
- {
- ++i;
- ++j;
- next[i]=j;
- }
- else
- j=next[j];
- }
- }//get_nextval
- int main(void)
- {
- int i,len,n;
- vector<int>ans;
- while(scanf("%s",pattern)!=EOF)
- {
- ans.clear();
- len=strlen(pattern);
- get_nextval(pattern);
- n=len;
- while(n)
- {
- ans.push_back(n);
- n=next[n];
- }
- if(pattern[0]==pattern[n-1]) //首部、尾部字符相同
- ans.push_back(1);
- i=ans.size()-1;
- for(;i>0;i--)
- printf("%d ",ans[i]);
- printf("%d\n",ans[0]);
- }
- return 0;
- }