KMP模板题汇总(更新中。。。)

博主由于编译器问题,无法使用next做全局变量,就使用了next1。

HDU1711

Number Sequence

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 31102 Accepted Submission(s): 13066

Problem Description
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

典型的KMP模板题,不过要用int来存数据

/*
 * hdu1711
 * 用int才能过的吗??
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std;

const int mmax = 1000005;

int next1[mmax];

int lens;
int lena;

void get_next(const int * a)
{
    int len = lena;
    int i,j;

    next1[0] = -1;
    i = 0;
    j = -1;
    while(i < len)
    {
        if (j == -1 || a[i] == a[j])
        {
            ++i;
            ++j;
            next1[i] = j;
        }
        else
            j = next1[j];
    }
}//get_next

int kmp(const int * s, const int * a, int pos)
{
    int i = pos;
    int j = 0;

    while(i < lens && j < lena)
    {
        if(j == -1 || s[i] == a[j])
        {
            ++i;
            ++j;
        }
        else
            j = next1[j];
    }
    if(j >= lena)
        return i-lena+1;
    else
        return -1;
}

int main()
{
    int s[mmax];
    int a[mmax];

    int t;

    while(scanf("%d", &t) == 1)
    {
        while(t--)
        {
            memset(a, 0, sizeof(a));
            memset(s, 0, sizeof(s));
            memset(next1, 0, sizeof(next1));

            scanf("%d %d", &lens, &lena);

            for (int i = 0; i < lens; i++)
            {
                scanf("%d", &s[i]);
            }

            for (int i = 0; i < lena; i++)
            {
                scanf("%d", &a[i]);
            }

            get_next(a);
//            for (int i = 0; i < lena; i++)
//            {
//                printf("%d\n", next1[i]);
//            }

            int index = kmp(s, a, 0);
            printf("%d\n", index);
        }
    }
    return 0;
}

HDU2087

剪花布条

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 22803 Accepted Submission(s): 14253

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

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

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

Sample Input
abcde a3
aaaaaa aa
#

Sample Output
0
3

前面那题稍微改一改,就是求个数了

/**
 * hdu2087
 * kmp练习2
 * 求子串个数
 * 方法:kmp第一种写法
 */
#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;
const int mmax = 1005;

int next1[mmax];
int lens;
int lena;

void get_next(char * a)
{
    int i = 0;
    int j = -1;
    next1[0] = -1;
    while(i < lena)
    {
        if(j == -1 || a[i] == a[j])
        {
            i++;
            j++;
            next1[i] == j;
        }
        else
            j = next1[j];
    }
}
int kmp(char * s, char * a, int index)
{
    int i = index;
    int j = 0;
    while(i < lens && j < lena)
    {
        if(j == -1 || s[i] == a[j])
        {
            i++;
            j++;
        }
        else
            j = next1[j];
    }
    if(j >= lena)
        return i - lena;
    return -1;
}

int main()
{
    char s[mmax];
    char a[mmax];
    memset(s, 0, sizeof(s));
    memset(a, 0, sizeof(a));

    while(scanf("%s", s) == 1)
    {
        if(s[0] == '#')
            break;
        scanf("%s", a);

        lens = strlen(s);
        lena = strlen(a);

        get_next(a);

        int sum = 0;
        int index= 0;
        while(index <= lens)
        {
            index = kmp(s, a, index);
            if(index == -1)
                break;
            index+=lena;
            sum++;
        }
        printf("%d\n", sum);

        memset(s, 0, sizeof(s));
        memset(a, 0, sizeof(a));
    }

    return 0;
}

HDU1867

A + B for you again

Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8284 Accepted Submission(s): 2067

Problem Description
Generally speaking, there are a lot of problems about strings processing. Now you encounter another such problem. If you get two strings, such as “asdf” and “sdfg”, the result of the addition between them is “asdfg”, for “sdf” is the tail substring of “asdf” and the head substring of the “sdfg” . However, the result comes as “asdfghjk”, when you have to add “asdf” and “ghjk” and guarantee the shortest string first, then the minimum lexicographic second, the same rules for other additions.

Input
For each case, there are two strings (the chars selected just form ‘a’ to ‘z’) for you, and each length of theirs won’t exceed 10^5 and won’t be empty.

Output
Print the ultimate string by the book.

Sample Input
asdf sdfg
asdf ghjk

Sample Output
asdfg
asdfghjk

看似不是kmp,实际就是kmp。
主要是字典序判断上要花点时间想一想,尽量不要把自己搞晕。

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;
const int mmax = 100005;


void get_next(char * s, int * next0)
{
    int i = 0;
    int j = -1;
    next0[0] = -1;
    int len0 = strlen(s);
    while(i < len0)
    {
        if(j == -1 || s[i] == s[j])
        {
            i++;
            j++;
            next0[i] = j;
        }
        else
            j = next0[j];
    }
}

int kmp(char * s, char * a, int * next0, int index)
{
    int i = index;
    int j = 0;
    next0[0] = -1;
    int lena = strlen(a);
    int lens = strlen(s);
    while(i < lens)
    {
        if(j == -1 || s[i] == a[j])
        {
            i++;
            j++;
        }
        else
            j = next0[j];
    }
    if(j == -1)
        return 0;
    else
        return j;
}

int main()
{
    char s1[mmax];
    char s2[mmax];
    int next1[mmax];
    int next2[mmax];

    while(scanf("%s %s", s1, s2) == 2)
    {
        get_next(s1, next1);
        get_next(s2, next2);

        int len1 = strlen(s1);
        int len2 = strlen(s2);

        int len12 = kmp(s1, s2, next2, 0);
        int len21 = kmp(s2, s1, next1, 0);

        char t1[mmax];
        char t2[mmax];
        int k = 0;
        for(int i = 0; i < len1; i++,k++)
            t1[k] = s1[i];
        for(int i = len12; i < len2; i++,k++)
            t1[k] = s2[i];
        t1[len1 + len2 - len12] = '\0';

        k = 0;
        for(int i = 0; i < len2; i++,k++)
            t2[k] = s2[i];
        for(int i = len21; i < len1; i++,k++)
            t2[k] = s1[i];
        t2[len1 + len2 - len21] = '\0';

        if(len12 > len21)
        {
            printf("%s\n", t1);
        }
        else if(len12 < len21)
        {
            printf("%s\n", t2);
        }
        else
        {
            int flag = 0;
            for(int i = 0; i < len1 + len2 - len12; i++)
            {
                if(t1[i] > t2[i])
                {
                    flag = 0;
                    break;
                }
                if(t1[i] < t2[i])
                {
                    flag = 1;
                    break;
                }
            }
            if(flag)
            {
                printf("%s\n", t1);
            }
            else
            {
                printf("%s\n", t2);
            }

        }
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值