码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 2203: 亲和串

时间:2017-09-23 12:14:26      阅读:109      评论:0      收藏:0      [点我收藏+]

标签: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");
}

}

HDU 2203: 亲和串

标签:show   ptr   weight   namespace   amp   typedef   acm   color   length   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!