亲和串

G - 亲和串
Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status
Description

人随着岁数的增长是越大越聪明还是越大越笨,这是一个值得全世界科学家思考的问题,同样的问题Eddy也一直在思考,因为他在很小的时候就知道亲和串如何判断了,但是发现,现在长大了却不知道怎么去判断亲和串了,于是他只好又再一次来请教聪明且乐于助人的你来解决这个问题。
亲和串的定义是这样的:给定两个字符串s1和s2,如果能通过s1循环移位,使s2包含在s1中,那么我们就说s2 是s1的亲和串。

 
Input

本题有多组测试数据,每组数据的第一行包含输入字符串s1,第二行包含输入字符串s2,s1与s2的长度均小于100000。

 
Output

如果s2是s1的亲和串,则输出"yes",反之,输出"no"。每组测试的输出占一行。

 
Sample Input

AABCD
CDAA
ASD
ASDF
Sample Output

yes
no
*/

 

  // 库函数算法*****

//#include <cmath>
//#include <ctime>
//#include <deque>
//#include <queue>
//#include <stack>
//#include <cctype>
//#include <sstream>
//#include <vector>
//#include <cassert>
//#include <cstdlib>
//#include <algorithm>
//#include <cstdio>
//#include <cstring>
#include <string>
#include <iostream>
using namespace std;
int main()
{
// freopen("in.txt","r",stdin);
 char a[100001],b[100001],c[200002];
 while(gets(a))
 {
  gets(b);
  strcpy(c,a);
  strcat(c,a);
  if(strstr(c,b)!=NULL && strlen(a)>=strlen(b))
   cout<<"yes"<<endl;
  else 
   cout<<"no"<<endl;
 }
 return 0;
}


点击打开链接http://acm.hdu.edu.cn/showproblem.php?pid=2203/*
//  KMP算法*******
/

#include <iostream>
#include <string>
#include <string.h>
#include <cstdio>
using namespace std;
const int N=100010;
int nextt[N],len1,len2,len;
char s[N],ss[N],str[2*N];
void getnext(){
  nextt[1]=0;
  int i=1,j=0;
  while(i<=len2){
	  if(j==0||ss[i]==ss[j]){
	    ++i;++j;
		if(ss[i]!=ss[j])
			nextt[i]=j;
		else
			nextt[i]=nextt[j];
	  }
	  else
		  j=nextt[j];
	 }
}
int kmp(){
  int i=1,j=1;
  while(i<=len&&j<=len2){
	 if(j==0||str[i]==ss[j]){
	    ++i;++j;
	  }
	  else
		  j=nextt[j];
   }
   if(j>len2)
	  return i-len2;
  return 0;
}
int main(){
    while(scanf("%s%s",s+1,ss+1)!=EOF){
	  memset(nextt,0,sizeof(nextt));
	  len1=strlen(s+1);
	  len2=strlen(ss+1);
	  if(len1<len2)
	  {printf("no\n");continue;}
	for(int i=1;i<=len1;++i)
		str[i]=s[i];
	len=len1;
	for(int i=1;i<=len1;++i)
		str[++len]=s[i];
	getnext();
	int flag=kmp();
	if(flag==0)
		printf("no\n");
	else
		printf("yes\n");
  }
  return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值