G

来源:互联网 发布:福建预算软件 编辑:程序博客网 时间:2024/05/15 05:13

以下是几种类型题:


A.类型1:匹配子字符串在母串中第几个位置开始出现。

B.类型2:子串在母串中出现了几次(可以有重复的)。

eg:子串:AZA

母串:AZAZAZA

ans = 3;

C.类型3:母串中最多有几个子串。

D.类型4:需要再补几个字符能构成一个类似手链那样循环相同的。

eg:abca ->2

abcde->5

aaa->0

E.类型5:给出一字符串,找出由2个或2个以上相同的子字符串组成的前缀,输出前缀长度及其相同的子字符串数。

F.类型6:当前最小循环节中字符串的个数(不可以补字符串)。

G.类型7:求最小循环节有几个。


Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = "" (the empty string) and a^(n+1) = a*(a^n).
Input
Each test case is a line of input representing s, a string of printable characters. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.
Output
For each s you should print the largest n such that s = a^n for some string a.
Sample Input
abcdaaaaababab.
Sample Output
143
Hint
This problem has huge input, use scanf instead of cin to avoid time limit exceed.



AC代码:


#include  <stdio.h>#include <iostream>#include <algorithm>#include <string.h>using namespace std;int pext[1000000] = {-5};char s[1000000];int len1;void get_next(char *T,int *next){int k = -1;int j = 0;pext[j] = k;while(j < len1){if((k == -1) || (T[j] == T[k])){k++;j++;pext[j] = k;}else{k = pext[k];}}}int main(){while(1){scanf("%s",s);if(strcmp(s,".") == 0){return 0;}else{int sum = 0;len1 = strlen(s);get_next(s,pext);int z;//最小循环节 z =  len1 - pext[len1];if(len1 % z ==0){for(int i=1;i<=len1;i++){if(i%z==0)sum++;}printf("%d\n",sum); }else{printf("1\n");}}}return 0;} 



原创粉丝点击