-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC++.cpp
More file actions
97 lines (89 loc) · 1.88 KB
/
Copy pathC++.cpp
File metadata and controls
97 lines (89 loc) · 1.88 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
#include <algorithm>
#include <iostream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;
unordered_map<string, int> cacheCountPair;
int countPair(string s, string pair)
{
string key = s + ':' + pair;
if (cacheCountPair.find(key) != cacheCountPair.end())
{
return cacheCountPair[key];
}
int count = 0;
for (int i = 0; i < s.size() - 1; i++)
{
if (s[i] == pair[0] && s[i + 1] == pair[1])
{
count++;
i++;
}
}
cacheCountPair[key] = count;
return count;
}
tuple<string, int> getMostCommonPair(string s)
{
int maxCount = -1;
string pair = "";
for (int i = 0; i < s.size() - 1; i++)
{
string _pair = s.substr(i, 2);
int count = countPair(s, _pair);
if (count > maxCount)
{
maxCount = count;
pair = _pair;
}
}
return {pair, maxCount};
}
string replace(string s, string pair, char bit)
{
string result = "";
for (int i = 0; i < s.size(); i++)
{
if (s[i] == pair[0] && s[i + 1] == pair[1])
{
result += bit;
i++;
}
else
{
result += s[i];
}
}
return result;
}
int main()
{
string s = "";
vector<string> transmutation;
int n;
int m;
cin >> n >> m;
cin.ignore();
for (int i = 0; i < n; i++)
{
string line;
cin >> line;
cin.ignore();
s += line;
}
tuple<string, int> pair = getMostCommonPair(s);
for (int i = 0; get<1>(pair) != 1; i++)
{
char c = 'Z' - i;
s = replace(s, get<0>(pair), c);
transmutation.push_back(string(1, c) + " = " + get<0>(pair));
pair = getMostCommonPair(s);
}
cout << s << endl;
for (string t : transmutation)
{
cout << t << endl;
}
}