-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPeriodic Strings.cpp
More file actions
143 lines (124 loc) · 3.97 KB
/
Periodic Strings.cpp
File metadata and controls
143 lines (124 loc) · 3.97 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <bits/stdc++.h>
using namespace std;
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
#define endl '\n'
#define sz(x) x.size()
#define PINF INT_MAX
#define NINF INT_MIN
#define PI (2.0*acos(0.0))
#define PB push_back
#define MP make_pair
#define all(x) x.begin(),x.end()
#define asort(x) sort(all(x));
#define dsort(x) sort(all(x), greater<int>())
#define unq(x) x.erase(unique(x.begin(),x.end()),x.end())
#define popcountl __builtin_popcountll
#define popcount __builtin_popcount
#define now cout<<"Here"<<endl;
#define mem(ara,val) memset(ara,val,sizeof(ara))
#define READ freopen("input.txt","r",stdin)
#define WRITE freopen("output.txt","w",stdout)
#define IOS ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0)
typedef long long LL;
typedef unsigned long long ULL;
typedef pair <int,int> PII;
typedef pair <LL,LL> PLL;
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
// *find_by_order = value of an index, order_of_key = index of a value where it should be
//typedef tree<PII, null_type, less<PII>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
inline bool checkBit(int N, int pos) { return (bool)(N & (1 << pos)); }
inline int setBit(int N, int pos) { return N = N | (1 << pos); }
inline int toggleBit(int N, int pos) { return N = (N ^ (1 << pos)); }
LL gcd(LL a, LL b) { return b==0 ? a : gcd(b, a % b); }
LL lcm(LL a, LL b) { return (a / gcd(a, b)) * b; }
//LL power_mod(LL base, LL power, LL mod){
// if(power==0) return 1LL;
// else if(power%2LL == 1){
// LL p1 = base % mod; LL p2 = (power_mod(base,power-1,mod))%mod;
// p1 = (LL)(p1*p2); return p1%mod;
// }
// else if(power%2LL == 0){
// LL p1 = (power_mod(base,power/2LL,mod))%mod; p1 = (LL)(p1*p1);
// return p1%mod;
// }
//}
//LL original_power(LL base, LL power){
// if(power==0) return 1LL;
// else if(power%2LL == 1){
// LL p1 = base; LL p2 = (original_power(base,power-1));
// p1 = (LL)(p1*p2); return p1;
// }
// else if(power%2LL == 0){
// LL p1 = (original_power(base,power/2LL)); p1 = (LL)(p1*p1);
// return p1;
// }
//}
//// Grid direction array [4]
//int X[4]= {0,0,-1,1};
//int Y[4]= {1,-1,0,0};
// Grid direction array [8]
//int X8[8]= {0,0,1,-1,-1,1,1,-1};
//int Y8[8]= {1,-1,0,0,-1,-1,1,1};
//// Knight Direction Array
//int KX[8] = {1,1,2,2,-1,-1,-2,-2};
//int KY[8] = {2,-2,1,-1,2,-2,1,-1};
///------------------------------------------------------------------------------------------------------
const int N = 2e6 + 10;
const int MOD = 1000000007;
int lps[N];
int buildKMP(string &text, int len)
{
int cnt = 0;
int n = text.size();
for(int i=1; i<n; i++)
{
int j = lps[i-1];
while(j > 0 and text[i] != text[j]) j = lps[j-1];
if(text[i] == text[j]) j++;
lps[i] = j;
if(j == len) cnt++;
}
return cnt;
}
void solve(int casenum)
{
string text;
cin>>text;
int text_len = text.size();
int ans = 0;
string temp;
for(int i=0; i<text_len; i++)
{
temp.PB(text[i]);
if(text_len % (i+1) == 0)
{
string news = temp + '#' + text;
int x = buildKMP(news,temp.size());
if(lps[news.size()-1]*x == text.size())
{
ans = i+1;
break;
}
}
}
cout<<ans<<endl;
}
int main()
{
IOS;
// READ;
// WRITE;
int t = 1;
cin>>t;
for(int tt=1; tt<=t; tt++){
cin.ignore();
solve(tt);
if(tt < t) cout<<endl;
}
return 0;
}
/*
https://onlinejudge.org/external/4/455.pdf
*/