对于 kmp的理解 基于poj题目

poj 3461、poj 2752、poj 2406、poj1961
Oulipo poj3461
The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter ‘e’. He was a member of the Oulipo group. A quote from the book:

Tout avait Pair normal, mais tout s’affirmait faux. Tout avait Fair normal, d’abord, puis surgissait l’inhumain, l’affolant. Il aurait voulu savoir où s’articulait l’association qui l’unissait au roman : stir son tapis, assaillant à tout instant son imagination, l’intuition d’un tabou, la vision d’un mal obscur, d’un quoi vacant, d’un non-dit : la vision, l’avision d’un oubli commandant tout, où s’abolissait la raison : tout avait l’air normal mais…

Perec would probably have scored high (or rather, low) in the following contest. People are asked to write a perhaps even meaningful text on some subject with as few occurrences of a given “word” as possible. Our task is to provide the jury with a program that counts these occurrences, in order to obtain a ranking of the competitors. These competitors often write very long texts with nonsense meaning; a sequence of 500,000 consecutive 'T’s is not unusual. And they never use spaces.

So we want to quickly find out how often a word, i.e., a given string, occurs in a text. More formally: given the alphabet {‘A’, ‘B’, ‘C’, …, ‘Z’} and two finite strings over that alphabet, a word W and a text T, count the number of occurrences of W in T. All the consecutive characters of W must exactly match consecutive characters of T. Occurrences may overlap.

Input
The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:

One line with the word W, a string over {‘A’, ‘B’, ‘C’, …, ‘Z’}, with 1 ≤ |W| ≤ 10,000 (here |W| denotes the length of the string W).
One line with the text T, a string over {‘A’, ‘B’, ‘C’, …, ‘Z’}, with |W| ≤ |T| ≤ 1,000,000.
Output
For every test case in the input file, the output should contain a single number, on a single line: the number of occurrences of the word W in the text T.

Sample Input
3
BAPC
BAPC
AZA
AZAZAZA
VERDI
AVERDXIVYERDIAN
Sample Output
1
3
0
题目大意为一个字符串在另一个字符串出现的次数

#include<cstdio>
#include<cstring>
using namespace std;
int next[1000001];
char a[1000001],b[1000001];
void getnext(char *s)
{
	int i=0,j=-1;
	next[0]=-1;
	int len=strlen(s);
	while(i<len)
	{
		if(j==-1||s[i]==s[j])
			next[++i]=++j;
		else
			j=next[j];
	}
}
int kmp(char *a,char *b)
{
    int total=0;
	int i=0,j=0;
	int l1=strlen(a);
	int l2=strlen(b);
	while(i<l1&&j<l2)
	{
		if(j==-1||a[i]==b[j])//j=-1确定了 第二个数组 从0开始遍历
		{
			i++;
			j++;
		}
		else
		{
			j=next[j];
		}
    if(j==l2)
    {
        j=next[j];、、这一步很重要
        total++;
    }
	}
return total;
}
int main()
{
	int n;
	scanf("%d",&n);
	while(n--)
	{
	    memset(next,0,sizeof(next));
		scanf("%s",a);
		scanf("%s",b);
		getnext(a);
		printf("%d\n",kmp(b,a));
	}
return 0;
}

Seek the Name, Seek the Fame poj 2752
The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give names to their newly-born babies. They seek the name, and at the same time seek the fame. In order to escape from such boring job, the innovative little cat works out an easy but fantastic algorithm:

Step1. Connect the father’s name and the mother’s name, to a new string S.
Step2. Find a proper prefix-suffix string of S (which is not only the prefix, but also the suffix of S).

Example: Father=‘ala’, Mother=‘la’, we have S = ‘ala’+‘la’ = ‘alala’. Potential prefix-suffix strings of S are {‘a’, ‘ala’, ‘alala’}. Given the string S, could you help the little cat to write a program to calculate the length of possible prefix-suffix strings of S? (He might thank you by giving your baby a name:)
Input
The input contains a number of test cases. Each test case occupies a single line that contains the string S described above.

Restrictions: Only lowercase letters may appear in the input. 1 <= Length of S <= 400000.
Output
For each test case, output a single line with integer numbers in increasing order, denoting the possible length of the new baby’s name.
Sample Input
ababcababababcabab
aaaaa
Sample Output
2 4 9 18
1 2 3 4 5
题目大意为给定若干字符串(这些字符串总长 ≤ 400000 ),在每个字符串中求出所有既是前缀又是后缀的子串长度。
主要考察 对next数组的运用

#include<cstdio>
#include<cstring>
using namespace std;
int next[1000001];
char s[1000001];
int a[1000001];
void getnext(char *s)
{
    int i=0,j=-1;
    next[0]=-1;
    int l=strlen(s);
    while(i<l)
    {
        if(j==-1||s[i]==s[j])
            next[++i]=++j;
        else
            j=next[j];
    }

}
int main()
{
    while(~scanf("%s",s))
    {
        memset(a,0,sizeof(a));
        memset(next,0,sizeof(next));
        getnext(s);
        int t=0;
        int len=strlen(s);
        int sum=next[len];
        while(sum>0)
        {
            a[++t]=sum;
            sum=next[sum];

        }
        for(int i=t;i>0;i--)
            printf("%d ",a[i]);
        printf("%d\n",len);

    }
}

Power Strings poj2460
Given two strings a and b we define ab to be their concatenation. For example, if a = “abc” and b = “def” then ab = “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
abcd
aaaa
ababab
.
Sample Output
1
4
3
Hint
This problem has huge input, use scanf instead of cin to avoid time limit exceed.
题目大意为 对于每组输入数据输出一行,找出每个字符串最多是由多少个相同的子字符串重复连接而成的
这是要考虑循环节 也就是n-next[n]; n%(n-next[n])来判断是否出现循环 如果是 一共有n/(n-next[n])

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int next[1000001];
char s[1000001];
int x;
void getnext(char *s)
{
	int i=0,j=-1;
	next[0]=-1;
	int len=strlen(s);
	while(i<len)
	{
		if(j==-1||s[i]==s[j])
			next[++i]=++j;
		else
			j=next[j];
	}
}
int main()
{
    int n;
    while(~scanf("%s",s))
    {
        memset(next,0,sizeof(next));
        if(s[0]=='.')
            break;
        int l=strlen(s);
        getnext(s);
        if(l%(l-next[l])!=0)
            printf("1\n");
        else
            printf("%d\n",l/(l-next[l]));
    }
return 0;
}

Period poj1961
For each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive), we want to know whether the prefix is a periodic string. That is, for each i (2 <= i <= N) we want to know the largest K > 1 (if there is one) such that the prefix of S with length i can be written as A K ,that is A concatenated K times, for some string A. Of course, we also want to know the period K.
Input
The input consists of several test cases. Each test case consists of two lines. The first one contains N (2 <= N <= 1 000 000) – the size of the string S.The second line contains the string S. The input file ends with a line, having the
number zero on it.
Output
For each test case, output “Test case #” and the consecutive test case number on a single line; then, for each prefix with length i that has a period K > 1, output the prefix size i and the period K separated by a single space; the prefix sizes must be in increasing order. Print a blank line after each test case.
Sample Input
3
aaa
12
aabaabaabaab
0
Sample Output
Test case #1
2 2
3 3

Test case #2
2 2
6 2
9 3
12 4
题目大意于上一个题基本相似

#include<cstdio>
#include<cstring>
using namespace std;
int next[1000001];
char a[1000001];
void getnext(char *s)
{
    int i=0,j=-1;
    next[0]=-1;
    int l=strlen(s);
    while(i<l)
    {
        if(j==-1||s[i]==s[j])
            next[++i]=++j;
        else
            j=next[j];
    }
}
int main()
{
    int n;
    int t=0;
    while(~scanf("%d",&n))
    {
        if(n==0)
            break;
        memset(next,0,sizeof(next));
        scanf("%s",&a);
        getnext(a);
        printf("Test case #%d\n",++t);
        for(int i=2;i<=n;i++)
        {
            int sum=i-next[i];
            if(i%sum==0&&i>sum)
                printf("%d %d\n",i,i/sum);

        }
        printf("\n");
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值