编程之类 字符串包含问题

来源:互联网 发布:网络手机卡资费对比 编辑:程序博客网 时间:2024/06/06 11:46
/* 
   字符串 s1 能否 通过 循环移位得到 字符串 s2
</pre><pre name="code" class="cpp">   如 s1="AC" s2="CACA"==>is true
      s1="ABCDEFR" s2="RA"==>is true;
      s1="ABCDEFR" s2="DFA" ==>is false;
*/
#include<iostream>using namespace std;bool IsContainer( char*str1, char*str2,int& startIndex){int len1=strlen(str1);int len2=strlen(str2);char* temp1=str1;char* temp2=str2;int i=0,j=0;bool flag=false;while(true){while(i<len1){if(temp1[i]==temp2[0]){int i_2=i;if(i_2==len1-1)i_2=0;else i_2++;j=1;bool exit=false;while(j<len2){if(i_2<len1 && temp1[i_2]!=temp2[j]){exit=true;break;}if(i_2==len1-1 && temp1[i_2]==temp2[j]){i_2=0;j++;continue;}i_2++;j++;}if(!exit){flag=true;startIndex=i+1;break;}}i=i+1;}if(flag){break;}if(i==len1)break;}return flag;}int main(){char*str1="AC";char*str2="CACA";int startIndex=0;if(IsContainer(str1,str2,startIndex)){cout<<"is true"<<"  startIndex= "<<startIndex<<endl;}else cout<<"is false"<<endl;char*str3="ABCDEFR";char*str4="RA";if(IsContainer(str3,str4,startIndex)){cout<<"is true"<<"  startIndex= "<<startIndex<<endl;}else cout<<"is false"<<endl;char*str5="ABCDEFR";char*str6="DFA";if(IsContainer(str5,str6,startIndex)){cout<<"is true"<<"  startIndex= "<<startIndex<<endl;}else cout<<"is false"<<endl;return 0;}

0 0