亲和串
2015-07-07 16:01
381 查看
http://acm.hdu.edu.cn/showproblem.php?pid=2203
#include<iostream>
#include<cstring>
using namespace std;
char s1[100010],s2[100010],c[200010],next1[100010];
void getnext(char *s2,int len2)
{
int i=0,j=-1;
next1[0]=-1;
while(i<len2)
{
if(j==-1 || s2[i]==s2[j])
{
i++;
j++;
next1[i]=j;
}
else
j=next1[j];
}
}
int main()
{//freopen("C:\\Users\\John\\Desktop\\hi.txt","r",stdin);
int i,j,ok;
while(cin>>s1>>s2)
{
memset(c,0,sizeof(c));
ok=0;
strcat(c,s1);
strcat(c,s1);
int len1=strlen(c);
int len2=strlen(s2);
if(len1<len2)
{
cout<<"no"<<endl;
continue;
}
getnext(s2,len2);
for(i=0,j=0;i<len1;)
{
if(j==-1 || c[i]==s2[j])
i++,j++;
else
j=next1[j];
if(j==len2)
{
ok=1;
break;
}
}
if(ok==1)
cout<<"yes"<<endl;
else
cout<<"no\n";
}
return 0;
}
http://acm.hdu.edu.cn/showproblem.php?pid=2203
#include<iostream>
#include<cstring>
using namespace std;
char s1[100010],s2[100010],c[200010],next1[100010];
void getnext(char *s2,int len2)
{
int i=0,j=-1;
next1[0]=-1;
while(i<len2)
{
if(j==-1 || s2[i]==s2[j])
{
i++;
j++;
next1[i]=j;
}
else
j=next1[j];
}
}
int main()
{//freopen("C:\\Users\\John\\Desktop\\hi.txt","r",stdin);
int i,j,ok;
while(cin>>s1>>s2)
{
memset(c,0,sizeof(c));
ok=0;
strcat(c,s1);
strcat(c,s1);
int len1=strlen(c);
int len2=strlen(s2);
if(len1<len2)
{
cout<<"no"<<endl;
continue;
}
getnext(s2,len2);
for(i=0,j=0;i<len1;)
{
if(j==-1 || c[i]==s2[j])
i++,j++;
else
j=next1[j];
if(j==len2)
{
ok=1;
break;
}
}
if(ok==1)
cout<<"yes"<<endl;
else
cout<<"no\n";
}
return 0;
}
相关文章推荐
- LeetCode-Min Stack-解题报告
- Mac 批量删除 .svn 文件
- 如何在spring官网上下载source code
- WPF 自定义属性和触发器
- [深入学习C#]C#实现多线程的方式:Task——任务
- MySQL查看和修改表的存储引擎
- Java——集合工具类和jdk1.5有关集合的新特性
- GRE写作必备句型
- SQL Server 跨服务器 不同数据库之间复制表的数据的方法!
- cas-server异常不会后台显示而是变成json显示在页面。
- php类的定义与继承用法实例
- 自定义“菊花dialog”
- Guide to understanding XSS – XSS payloads, attack vectors, BeEF hooking, MiTM with Shank and some hi
- AIX Study之--AIX用户资源访问限制配置
- 不用浏览器,直接用代码发送文件给webservices所在服务器 并且可以周期行的发送
- AsyncTask的使用
- leetcode - Reverse Words in a String
- leetcode - Reverse Words in a String
- 硬件电路之电感,磁珠,零欧姆电阻的应用
- LeetCode89:Gray Code