编程之类 字符串包含问题

/* 
   字符串 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值