标签:show ptr weight namespace amp typedef acm color length
///@date 9/23/2017
///@author Sycamore
///@link http://acm.hdu.edu.cn/showproblem.php?pid=2203
#include<bits/stdc++.h>
using namespace std;
typedef vector<int>VI;
VI getPi(const string& p)
{
VI pi = VI(p.length());
int k = -2;
for (int i = 0; i < p.length(); i++)
{
//now k==p[i-1]
while (k >= -1 && p[k + 1] != p[i])//find pi[i] using pi[i-1]
k = (k == -1) ? -2 : pi[k];//make shifts
pi[i] = ++k;
}
return pi;
}
bool KMP(const string& t, const string& p)
{
VI pi = getPi(p);
int k = -1;
for (int i = 0; i < t.length(); i++)
{
while (k >= -1 && p[k + 1] != t[i])
k = (k == -1) ? -2 : pi[k];
k++;
if (k == p.length() - 1) {
return true;
}
}
return false;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
string s,t;
while(cin>>s>>t)
{
s+=s;
cout<<(KMP(s,t)?"yes\n":"no\n");
}
}
标签:show ptr weight namespace amp typedef acm color length
原文地址:http://www.cnblogs.com/zjnu/p/7580625.html