-
Notifications
You must be signed in to change notification settings - Fork 2
/
10298.cpp
executable file
·93 lines (81 loc) · 2.02 KB
/
10298.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*The code is modify from 455.cpp */
#include <iostream>
#include <iomanip>
using namespace std ;
#include <cstring>
#define MaxChars 1000000
int
Periodic_Strings( const char *buffer )
{
int repetitions = 0 ;
int Period = 0 ;
int i = 0 ; // loop counter
const int lenBuffer = strlen( buffer ) ;
char subString[ lenBuffer + 1 ] ;
for( Period = 1 ; Period * Period < lenBuffer ; Period++ )
{
if( lenBuffer % Period == 0 )
{
strncpy( subString, buffer, Period ) ;
subString[ Period ] = '\0' ;
for( i = 0 ; i < lenBuffer ; i += Period )
{
if( strncmp( buffer + i, subString, Period ) != 0 )
// Compare buffer with the subString.
// If subString is not the same as (buffer+i), this is not
// a peroid substring of buffer.
{
break ;
}
}
if( i == lenBuffer )
// Now, subString is a substring of buffer(Also, the smallest!!).
{
return Period ;
}
}
}
for( repetitions = 2 ; repetitions * repetitions <= lenBuffer ; repetitions++ )
{
if( lenBuffer % repetitions == 0 )
{
Period = lenBuffer / repetitions ;
strncpy( subString, buffer, Period ) ;
subString[ Period ] = '\0' ;
for( i = 0 ; i < lenBuffer ; i += Period )
{
if( strncmp( buffer + i, subString, Period ) != 0 )
// Compare buffer with the subString.
// If subString is not the same as (buffer+i), this is not
// a peroid substring of buffer.
{
break ;
}
}
if( i == lenBuffer ) // Now, subString is a substring of buffer.
{
return Periodic_Strings( subString ) ;
// Keep on finding the smallest period of the input string.
}
}
}
return lenBuffer ;
// If period can't be find on above two loops, that means the smallest
// period of the input string is itself.
}
int
main()
{
int N = 0 ;
int i = 0 ; // loop counter
char buffer[ MaxChars + 1 ] ;
while( cin.getline( buffer, MaxChars + 1 ) )
{
if( buffer[ 0 ] == '.' && strlen( buffer ) == 1 )
{
break ;
}
cout << strlen( buffer ) / Periodic_Strings( buffer ) << endl ;
}
return 0 ;
}