首页 > 其他分享 >777. 字符串乘方

777. 字符串乘方

时间:2022-10-29 16:13:04浏览次数:49  
标签:777 tem int 样例 len substr 乘方 字符串


文章目录

  • ​​Question​​
  • ​​Ideas​​
  • ​​Code​​

Question

给定两个字符串 a 和 b,我们定义 a×b 为他们的连接。

例如,如果 a=abc 而 b=def, 则 a×b=abcdef。

如果我们将连接考虑成乘法,一个非负整数的乘方将用一种通常的方式定义:a0=``(空字符串),a(n+1)=a×(an)。

输入格式
输入包含多组测试样例,每组测试样例占一行。

每组样例包含一个字符串 s,s 的长度不超过 100,且不包含空格。

最后的测试样例后面将是一个点号作为一行。

输出格式
对于每一个 s,你需要输出最大的 n,使得存在一个字符串 a,让 s=an。

输入样例:
abcd
aaaa
ababab
.
输出样例:
1
4
3

Ideas

Code

#include <iostream>
#include <string>

using namespace std;

int main()
{
string s;
while (cin >> s, s != ".")
{
int len = s.size(); // 字符串长度
for (int n = len; n; n--)
{
if (len % n == 0) // 约数
{
int m = len / n; // 字串长度
string tem;
string substr = s.substr(0, m);
for (int i = 0; i < n; i++)
{
tem += substr;
}

if (tem == s)
{
cout << n << endl;
break;
}
}
}
}

return 0;
}


标签:777,tem,int,样例,len,substr,乘方,字符串
From: https://blog.51cto.com/u_14608932/5806191

相关文章