*KMP

今天是2017/5/22,DCDCBigBig的第五篇博文

啊。。。最近真是太忙了,又要腐敗(劃掉)又要複習期末考,就先集體把代碼發上來,順便把詩唸完。看博客的童鞋請注意,表*的博文就代表只有代碼,沒有思想。思想什麼的等暑假在一起寫吧。。。

KMP

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
int len,len1,next[100001];
char st[10000],st1[10000];
bool fid;
int main(){
    fid=false;
    scanf("%s",st);
    scanf("%s",st1);
    memset(next,0,sizeof(next));
    next[0]=-1;
    len=strlen(st);
    len1=strlen(st1);
    for(int i=1,j=0;i<len;i++){
        for(j=next[i-1];j!=-1&&st[j+1]!=st[i];j=next[j]);
        if(st[j+1]==st[i])j++;
        next[i]=j;
    }
    for(int i=0,j=-1;i<len1;i++){
        for(;j!=-1&&st[j+1]!=st1[i];j=next[j]);
        if(st[j+1]==st1[i])j++;
        if(j==len-1){
            printf("YES\n");
            fid=true;
            break;
        }
    }
    if(!fid){
        printf("NO\n");
    }
    return 0;
}

發佈了36 篇原創文章 · 獲贊 3 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章