字符串循环移位包含问题

#include <iostream>
#include <string>
#include <stdio.h>
#include <time.h>

using namespace std;

void PrintCharArray(string a);
bool CharContain(string src, string des);

int main(int argc, char *argv[])
{
	/*
	*  字符串的移位包含问题:给定两个字符串s1和s2,要求判断s2是否能够被s1做循环移位(rotate)得到的字符串包含。
	*  eg: s1 = AABCD, s2 = CDAA, 返回true; s1 = ABCD, s2 = ACBD, 返回false.
	*  注意s1循环移位所有可能包含于s1s1中,因此,可以用string的find方法判断s1s1中是否包含s2.
	*/

	//string src = "AABBCD";
	//string des = "CDAA";
	//PrintCharArray(src);
	//PrintCharArray(des);
	//CharContain(src, des);

	clock_t start_time = clock();

	CharContain("AABBCD", "CDAA");
	CharContain("ABCD", "ACBD");

	clock_t end_time = clock();

	cout << "Running time of CharContain1 is " 
		<< static_cast<double>(end_time - start_time) / CLOCKS_PER_SEC * 1000
		<< "ms." << endl;
	
	return 0;
}

void PrintCharArray(string a)
{
	if (!a.empty())
	{
		int len = a.size();
		for (int i = 0; i < len; i++)
		{
			cout << a[i];
		}
	}
	cout << endl;
}

bool CharContain(string src, string des)
{
	int len = src.size();
	string tmp = src + src;

	int index = tmp.find(des);

	if (index != -1)
	{
		cout << "Case One: " << endl;
		for (int i = index; i < len * 2; i++)
		{
			cout << tmp[i];
		}
		cout << endl;
		cout << "s2 in s1s1." << endl << endl;
		return true;
	}
	else
	{
		cout << "Case Two: " << endl;
		cout << "s2 not in s1s1." << endl;
		return false;
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Digital2Slave

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值