-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSPOJ15563.cc
67 lines (60 loc) · 1.36 KB
/
SPOJ15563.cc
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
// SPOJ 15563: Check the string Powers
// http://www.spoj.com/problems/IITKWPCJ/
//
// Solution: sorting, rolling hash
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <functional>
#include <algorithm>
using namespace std;
#define ALL(c) c.begin(), c.end()
#define FOR(i,c) for(typeof(c.begin())i=c.begin();i!=c.end();++i)
#define REP(i,n) for(int i=0;i<n;++i)
#define fst first
#define snd second
typedef unsigned long long ULL;
struct RollingHash {
static const ULL p = 1000000007ULL;
string s;
int n;
vector<ULL> pow, phash;
RollingHash(string s) : s(s), n(s.size()), pow(n+1), phash(n+1) {
pow[0] = 1; phash[0] = 0;
REP(i, n) {
phash[i+1] = s[i] + phash[i] * p;
pow[i+1] = pow[i] * p;
}
}
// hash of s[i,j)
ULL hash(int i, int j) {
return phash[j] - phash[i] * pow[j-i];
}
ULL hash(string t) {
ULL h = 0;
REP(i, t.size()) h = t[i] + h * p;
return h;
}
};
bool solve(string s, string t) {
if (s.size() > t.size()) swap(s, t);
int n = s.size(), m = t.size();
RollingHash RH(t+t);
ULL h = RH.hash(s);
int i = 0;
do {
if (h != RH.hash(i, i+n)) return false;
i = (i + n) % m;
} while (i > 0);
return true;
}
int main() {
int n; cin >> n;
while (n--) {
string s, t;
cin >> s >> t;
cout << (solve(s, t) ? "YES" : "NO") << endl;
}
}