首页 > 其他 > 详细

HDU 2203: 亲和串

时间:2017-09-23 11:46:22      阅读:222      评论:0      收藏:0      [点我收藏+]
///@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");
}

}

HDU 2203: 亲和串

原文:http://www.cnblogs.com/zjnu/p/7580625.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!