首页 > 其他 > 详细

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

时间:2019-08-18 15:30:59      阅读:141      评论:0      收藏:0      [点我收藏+]
//判断输入的字符串是不是可以由子串多次重复构成
#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;
}

  

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

原文:https://www.cnblogs.com/277223178dudu/p/11372282.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!