您的位置:首页 > 其它

POJ 3461和COJ 1248 KMP算法

2013-10-11 17:31 260 查看

COJ 1248

【算法实验项目】串匹配问题

Time Limit: 1000 ms Memory Limit: 65536 KB

Total Submit: 31 Accepted: 5

Description
给定一个文本,在该文本中查找并定位任意给定字符串。

Input
有两行。

第一行为主串S,(长度大于1,不大于1000000)

第二行为子串T,(长度大于1,不大于10000)

Output
输出只有一行。

如果子串在母串中出现,输出所有能和子串匹配的母串的起始位置。并以空格隔开(位置从0开始)

如果子串不存在于母串中输出-1.

Sample Input
abcbcbcabcb

bcb

Sample Output
1 3 8

Hint
从母串的第1、3、8位开始,都可以和子串匹配

推荐KMP算法

思路:KMP算法……慢慢来,把KMP吃透了……

#include <iostream>
#include <map>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
char str[1000005],str1[10005];
int next[10005],len,len1,p=1;
void getnext()
{
    int k=-1,j=0;
    next[0]=-1;
    while(j<len1)
    {
        if(k==-1||str1[j]==str1[k])
        {
            j++;
            k++;
            if(str1[j]!=str1[k]) next[j]=k;
            else next[j]=next[k];
        }
        else k=next[k];
    }
}
void kmp()
{
    int i=0,j=0,k=0;
    while(i<len)
    {
        if(str1[j]==str[i])
        {
            i++;
            j++;
        }
        else
        {
            j=next[j];
            if(j==-1)
            {
                i++;
                j=0;
            }
        }
        if(j==len1) {printf("%d ",i-len1);p=0;}
    }
}
int main()
{
    scanf("%s%s",str,str1);
    len=strlen(str);
    len1=strlen(str1);
    getnext();
    kmp();
    if(p) printf("-1\n");
    else printf("\n");
    return 0;
}

POJ 3461

方法与上题相同……

#include <iostream>
#include <map>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
char str[1000005],str1[10005];
int next[10005],len,len1,sum;
void getnext()
{
    int k=-1,j=0;
    next[0]=-1;
    while(j<len1)
    {
        if(k==-1||str1[j]==str1[k])
        {
            j++;
            k++;
            if(str1[j]!=str1[k]) next[j]=k;
            else next[j]=next[k];
        }
        else k=next[k];
    }
}
void kmp()
{
    int i=0,j=0,k=0;
    while(i<len)
    {
        if(str1[j]==str[i])
        {
            i++;
            j++;
        }
        else
        {
            j=next[j];
            if(j==-1)
            {
                i++;
                j=0;
            }
        }
        if(j==len1) sum++;
    }
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        sum=0;
        scanf("%s%s",str1,str);
        len=strlen(str);
        len1=strlen(str1);
        getnext();
        kmp();
        cout<<sum<<endl;
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: