-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path455-Periodic Strings.cpp
More file actions
80 lines (60 loc) · 1.39 KB
/
455-Periodic Strings.cpp
File metadata and controls
80 lines (60 loc) · 1.39 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
/*----------------------|
/ Author : Ashraf Tasin |
/ Date : 01.04.19 |
/----------------------*/
#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ll long long
#define all v.begin(),v.end()
#define flash ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define db double
#define endl "\n"
#define mnm pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>
#define show for(int i=0;i<v.size();i++) cout << v[i] << " ";
#define eps 1e-6
#define mx 100004
using namespace std;
/*King Moves
int dx[]={-1,1,-1,0,1,-1,0,1};
int dy[]={0,0,-1,-1,-1,1,1,1}; */
/*Knight Moves
int dx[] = { 2, 1, -1, -2, -2, -1, 1, 2 };
int dy[] = { 1, 2, 2, 1, -1, -2, -2, -1 }; */
/*Normal Moves
int x[] = {0, 1, 0, -1};
int y[] = {-1, 0, 1, 0}; */
int n;
void solve(string pat)
{
int table[n+15];
int j;
table[0]=-1;
for(int i=1;i<=n;++i)
{
j=table[i-1];
while(j>=0 and pat[i-1]!=pat[j]) j=table[j];
table[i]=j+1;
}
if(table[n]==0) {cout << n << endl;return;}
if(n%(n-table[n])==0) cout << n-table[n] << endl;
else cout << n << endl;
return;
}
int main()
{
int tc;
cin >> tc;
string x;
getline(cin,x);
while(tc--)
{
string pat;
cin >> pat;
n=pat.size();
if(n==1) cout << 1 << endl;
else solve(pat);
if(tc) cout << endl;
}
return 0;
}