-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path1580.cc
68 lines (60 loc) · 1.67 KB
/
1580.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
68
//Name: String Matching
//Level: 2
//Category: 文字列比較
//Note:
/*
* 片方の文字列を1文字ずつずらしながら比較していけば良い。
* インデックスの取り方に注意。範囲外アクセスや端の数え忘れはありがち。
*/
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int gcd(int a, int b) {
if(a < b) return gcd(b, a);
while(b) {
int tmp = a % b;
a = b;
b = tmp;
}
return a;
}
int main() {
while(true) {
string str1, str2;
cin >> str1 >> str2;
if(str1 == "-1") break;
vector<int> charpos[26];
for(int i = 0; i < str1.length(); ++i) {
charpos[str1[i]-'A'].push_back(i);
}
int bestcnt = 0;
for(int i = 0; i < str2.length(); ++i) {
int idx = str2[i]-'A';
if(charpos[idx].empty()) continue;
for(int j = 0; j < charpos[idx].size(); ++j) {
int s1p = charpos[idx][j];
int cnt = 0;
for(int k = 0; (i+k)<str2.length() && (s1p+k)<str1.length(); ++k) {
if(str1[s1p+k] == str2[i+k]) ++cnt;
}
if(cnt > bestcnt) bestcnt = cnt;
}
}
int len = str1.length() + str2.length();
bestcnt *= 2;
cout << "appx(" << str1 << "," << str2 << ") = ";
if(len == bestcnt) {
cout << 1 << endl;
}
else if(bestcnt == 0) {
cout << 0 << endl;
}
else {
int g = gcd(len, bestcnt);
cout << bestcnt/g << "/" << len/g << endl;
}
}
return 0;
}