欢迎来到IT嘟嘟的博客

人生三从境界:昨夜西风凋碧树,独上高楼,望尽天涯路。 衣带渐宽终不悔,为伊消得人憔悴。 众里寻他千百度,蓦然回首,那人却在灯火阑珊处。
扩大
缩小

判断输入的字符串是不是可以由子串多次重复构成

//判断输入的字符串是不是可以由子串多次重复构成
#include<iostream>
#include<string>
using namespace std;
class Solution {
public:
	bool repeated(string s) 
	{
		int n = s.size();
		string temp1 = "";
		string temp2 = "";
		for (int i = 2; i <= n; ++i)
		{
			if (n % i == 0)
			{
				int length = n / i;
				temp1 = string(s.begin(), s.begin() + length);
				for (int j = 0; j < i; ++j)
					temp2 += temp1;
			}
			if (temp2 == s)
				cout << temp1 << endl;
				return true;
			
			temp2 = "";
		}
		return false;
	}
};
int main()
{
	string str;
	cin >> str;
	//Solution().repeated(str);
	cout << Solution().repeated(str) << endl;
	system("pause");
	return 0;
}

  

posted on 2019-08-18 14:15  IT嘟嘟  阅读(463)  评论(0编辑  收藏  举报

导航