赵括

导航

忽略大小写的模式匹配

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<cmath>
#include<cstring>
#include<stdlib.h>
#include<iomanip>
#include<ctype.h>
#include<vector>
#include<string>

using namespace std;

void getNext(string &p,vector<int> &next)
{
    next.resize(p.size());
    next[0] = -1;
    
    int i=0,j=-1;
    
    while(i!= p.size()-1)
    {
        if(j==-1||p[i]==p[j])
        {
            ++i;
            ++j;
            next[i] = p[i]!=p[j]?j:next[j];
        }
        else
            j=next[j];
    }
}

int kmp(string str1,string str2)
{
    vector<int> next(str1.size());
        getNext(str1,next);
        int i=0,j=0;
        while(i!=str2.length() && j!= str1.length())
        {
            if(j==-1||str2[i]==str1[j])
            {
                ++i;
                ++j;
            }
            else
                j=next[j];
        }
        return j==str1.length()?i-j:-1;
}

int main()
{
    string str1,str2;
    while(cin >> str1 >> str2)
    {
        transform(str1.begin(),str1.end(),str1.begin(),::tolower);
        transform(str2.begin(),str2.end(),str2.begin(),::tolower);
            
        if(kmp(str1,str2)==-1)
            cout << "NO" << endl;
        else
            cout << "YES" << endl;
    }
}

 

posted on 2015-06-18 10:37  赵括  阅读(268)  评论(0)    收藏  举报