hdu2203

来源:互联网 发布:java面试宝典2016下载 编辑:程序博客网 时间:2024/05/21 10:52

题意:给定两个字符串s1和s2,如果能通过s1循环移位,使s2包含在s1中,那么我们就说s2 是s1的亲和串。如果s2是s1的亲和串,则输出"yes",反之,输出"no"。每组测试的输出占一行。可以直接调用库函数。
/*

Sample Input
AABCD
CDAA
ASD
ASDF
Sample Output
yes
no

*/

#include<iostream>
#include<string>
using namespace std;
int main()
{
    string s1,s2;
    while(getline(cin,s1))
    {
        s1.append(s1);//把 s1接到原来s1后面
        getline(cin,s2);
        if(s1.find(s2)==-1)//s1中是否存在子串s2
            cout<<"no"<<endl; 
        else
            cout<<"yes"<<endl;
    }
    return 0;
}
另一个没有调用的

#include<iostream>
#include<string>
using namespace std;
char str1[100005],str2[100005];
int Find(int x,int y,char ch)
{
    int i;
    for(i=x;i<y;i++)
    {
      if(str1[i]==ch)
      return i;
    }
    return -1;
}
int main()
{
    int len1,len2,i,j,k;
    while(cin>>str1>>str2)
    {
       len1=strlen(str1);
       len2=strlen(str2);
       if(len1<len2)
       {
          cout<<"no"<<endl;
          continue;
       }
       else
       {
           i=-1;
           while(i<len1)
           {
              i=Find(i+1,len1,str2[0]);
              if(i==-1)
              {
                 cout<<"no"<<endl;
                 break;
              }
              else
              {
                 k=i;
                 for(j=0;j<len2;j++)
                 {
                    if(str1[k]!=str2[j])
                      break;
                    if(k==len1-1)
                      k=0;
                    else
                       k++;
                  }
                  if(j==len2)
                  {
                    cout<<"yes"<<endl;
                    break;
                  }
              }
           }
       }
       str1[0]='/0';
       str2[0]='/0';
    }
     return 0;
}

 

原创粉丝点击