-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathicpc1585.cpp
More file actions
49 lines (44 loc) · 1.1 KB
/
icpc1585.cpp
File metadata and controls
49 lines (44 loc) · 1.1 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
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int gcd(int x, int y){
return (x % y) ? gcd(y, x % y) : y;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, answer = 0;
vector<int> failure;
vector<char> roulette, meat;
cin >> n;
roulette.resize(n * 2);
meat.resize(n);
failure.resize(n);
for (int i = 0; i < n; i++) {
cin >> roulette[i];
roulette[n + i] = roulette[i];
}
for(int i = 0; i < n; i++)
cin >> meat[i];
for(int i = 1, f = 0; i < n; i++){
while(f > 0 && meat[i] != meat[f])
f = failure[f - 1];
if(meat[i] == meat[f])
failure[i] = ++f;
}
for(int i = 0, f = 0; i < roulette.size() - 1; i++){
while(f > 0 && roulette[i] != meat[f])
f = failure[f - 1];
if(roulette[i] == meat[f]){
if(++f == n){
answer++;
f = failure[f - 1];
}
}
}
int g = gcd(answer, n);
cout << answer / g << '/' << n / g << endl;
return 0;
}