HDU 2203 亲和串

简单,使用strstr()就可以了;

#include<stdio.h>
#include<string.h>
#define MAX 100001
int main()
{
 char s1[MAX];
 char s2[MAX];
 char s3[2*MAX];
 while ( gets(s1) )
 {
  gets(s2);
  strcpy(s3, s1);
  strcat(s3, s1);
  if ( strstr(s3, s2) != NULL ) 
   puts("yes");
  else
   puts("no"); 
 }
 return 0;
}

此题简单,可用于KMP算法练手

#include<iostream>
#include<string>
#define MAX 100003
int lens1;
int lens2;
int index;
int next[MAX];
using namespace std;
void my_overlay(const string & s2)
{
 int i;
 next[0]=-1;
 for(i=1;i<lens2;i++)
 {
   index=next[i-1];
   while(index>=0&&s2[i]!=s2[index+1])
      index=next[index];
   if(s2[i]==s2[index+1])
      next[i]=index+1;
   else next[i]=-1;
 }
}
void my_kmp(const string & s1,const string & s2)
{
 
 int i,j=0,count=0;
 for(i=0;i<lens1;)
 {
  if(s1[i]==s2[j])
  {
   j++;i++;
  }
  else
  {
     if(j==0) i++;
     else j=next[j-1]+1;
  }
  if(j==lens2) {count++;j=0;}
 }
 if(count>0) cout<<"yes"<<endl;
 else cout<<"no"<<endl;


}
int main()
{
 string s1,s2,s3;
 while(cin>>s1>>s2)
 {
  lens1=s1.length();
    lens2=s2.length();
    if(lens2>lens1) cout<<"no"<<endl;
    else
    {
       s3=s1;
    s1=s1+s3;
          lens1=s1.length();
    my_overlay(s2);
    my_kmp(s1,s2);  
    }
 }
   return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值