kmp算法几个简单题(hdu1358,poj3461,hdu1771,hdu2087)

A - Period

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 file 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<bits/stdc++.h>
#define N 1000025
using namespace std;
int n,m;
char p[N];
int next2[N];

void getnext()
{
    int i=0,j=-1;
    next2[0]=-1;
    while(i<m)
    {
        if(j==-1||p[i]==p[j])
        {
            next2[++i]=++j;
        }
        else {
                j=next2[j];
        }
    }
    for(int i=1;i<=m;i++)
        printf("%d ",next2[i]);
}

int main()
{
    int cas=1;
    while(scanf("%d",&m)&&m!=0)
    {
        scanf("%s",&p);
        getnext();
        printf("Test case #%d\n",cas++);

        for(int i=2;i<=m;i++)
        {
            int m=i-next2[i];
            if(i%m==0&&m!=i)
            {
                printf("%d %d\n",i,i/m);
            }
        }
        printf("\n");

    }
    return 0;
}

B - Oulipo

 

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<bits/stdc++.h>//poj3461
#define N 1000025
using namespace std;
int n,m;
char s[N],p[N];
int next2[N];

void getnext()
{
    int i=0,j=-1;
    next2[0]=-1;
    m=strlen(p);
    while(i<m)
    {
        if(j==-1||p[i]==p[j])
        {
            next2[++i]=++j;
        }
        else {
                j=next2[j];
        }
    }

}

int kmp()
{
    int i=0,j=0,sum=0;
    getnext();
    n=strlen(s);
    while(i<n)
    {
        if(j==-1||s[i]==p[j])
        {
            j++;
            i++;
        }
        else j=next2[j];
        if(j==m)
        {
            sum++;
            j=next2[j];
        }
    }
    return sum;
}

int main()
{
    int t;
        scanf("%d",&t);
    while(t--)
    {
        scanf("%s%s",&p,&s);
        printf("%d\n",kmp());
    }
    return 0;
}

C - Number Sequence

 

Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M <= 10000, 1 <= N <= 1000000). Your task is to find a number K which make a[K] = b[1], a[K + 1] = b[2], ...... , a[K + M - 1] = b[M]. If there are more than one K exist, output the smallest one. 

Input

The first line of input is a number T which indicate the number of cases. Each case contains three lines. The first line is two numbers N and M (1 <= M <= 10000, 1 <= N <= 1000000). The second line contains N integers which indicate a[1], a[2], ...... , a[N]. The third line contains M integers which indicate b[1], b[2], ...... , b[M]. All integers are in the range of [-1000000, 1000000]. 

Output

For each test case, you should output one line which only contain K described above. If no such K exists, output -1 instead. 

Sample Input

2
13 5
1 2 1 2 3 1 2 3 1 3 2 1 2
1 2 3 1 3
13 5
1 2 1 2 3 1 2 3 1 3 2 1 2
1 2 3 2 1

Sample Output

6
-1
#include<bits/stdc++.h>//hdu1711
#define N 1000025
using namespace std;
int n,m;
int s[N],p[N];
int next2[N];

void getnext()
{
    int i=0,j=-1;
    next2[0]=-1;
    while(i<m)
    {
        if(j==-1||p[i]==p[j])//j==-1,j已经是最左边的了,所以只能i往右移
        {
            next2[++i]=++j;//如果p[i]==p[j],呢么在已知p[i-1]==p[j-1]的情况下,next2[i]=next2[i-1];如果p[i-1]!=p[j-1],呢么j=next2[j];
        }
        else {
                j=next2[j];
        }
    }
}

int kmp()
{
    int i=0,j=0;
    getnext();
    while(i<n)
    {
        if(j==-1||s[i]==p[j])
        {
            j++;
            i++;
        }
        else j=next2[j];
        if(j==m)//已经匹配的长度j和p的长度m相等,就返回i
            return i;
    }
    return -1;
}

int main()
{
    int t;
        scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
            scanf("%d",&s[i]);
        for(int i=0;i<m;i++)
            scanf("%d",&p[i]);
        if(kmp()==-1)
            printf("-1\n");
        else printf("%d\n",kmp()-m+1);
    }
    return 0;
}

D - 剪花布条

 

一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案。对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢? 

Input

输入中含有一些数据,分别是成对出现的花布条和小饰条,其布条都是用可见ASCII字符表示的,可见的ASCII字符有多少个,布条的花纹也有多少种花样。花纹条和小饰条不会超过1000个字符长。如果遇见#字符,则不再进行工作。 

Output

输出能从花纹布中剪出的最多小饰条个数,如果一块都没有,那就老老实实输出0,每个结果之间应换行。 

Sample Input

abcde a3
aaaaaa  aa
#

Sample Output

0
3
#include<bits/stdc++.h>
#define N 1000025
using namespace std;
int n,m;
char s[N],p[N];
int next2[N];

void getnext()
{
    int i=0,j=-1;
    next2[0]=-1;
    m=strlen(p);
    while(i<m)
    {
        if(j==-1||p[i]==p[j])
        {
            next2[++i]=++j;
        }
        else {
                j=next2[j];
        }
    }

}

int kmp()
{
    int i=0,j=0,sum=0;
    getnext();
    n=strlen(s);
    while(i<n)
    {
        if(j==-1||s[i]==p[j])
        {
            j++;
            i++;
        }
        else j=next2[j];
        if(j==m)
        {
            sum++;
            j=0;//初始0,重新计数
        }
    }
    return sum;
}

int main()
{
    while(1)
    {
        scanf("%s",&s);
        if(s[0]=='#')
            break;
        scanf("%s",&p);
        printf("%d\n",kmp());
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/wangtao971115/p/10358275.html

发布了0 篇原创文章 · 获赞 18 · 访问量 6万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览