码迷,mamicode.com
首页 > 其他好文 > 详细

Power Strings

时间:2019-06-29 19:19:49      阅读:71      评论:0      收藏:0      [点我收藏+]

标签:print   efi   lin   example   closed   lap   pen   while   nbsp   

Power Strings

题目描述

Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = "" (the empty string) and a^(n+1) = a*(a^n).
 

输入

Each test case is a line of input representing s, a string of printable characters. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.
 

输出

For each s you should print the largest n such that s = a^n for some string a.
 

样例输入

abcd
aaaa
ababab
.

样例输出

1
4
3

技术图片
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define MAXN 1000017
 4 int Next[MAXN];
 5 int len;
 6 void getNext( char T[])
 7 {
 8     int i = 0, j = -1;
 9     Next[0] = -1;
10     while(i <len)
11     {
12         if(j == -1 || T[i] == T[j])
13         {
14             i++,j++;
15             Next[i] = j;
16         }
17         else
18             j = Next[j];
19     }
20 }
21 
22 int main()
23 {
24     char ss[MAXN];
25     int length;
26     while(cin >> ss)
27     {
28         if(ss[0] == .)
29             break;
30         len = strlen(ss);
31         getNext(ss);
32         length = len - Next[len];
33         if(len%length == 0)
34             cout << len/length << endl;
35         else
36             cout << "1" << endl;
37     }
38     return 0;
39 }
View Code

 

Power Strings

标签:print   efi   lin   example   closed   lap   pen   while   nbsp   

原文地址:https://www.cnblogs.com/qing123tian/p/11107478.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!