親和串

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;
}


發佈了45 篇原創文章 · 獲贊 6 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章