-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathMorgan and a string.cpp
More file actions
129 lines (127 loc) · 3.13 KB
/
Morgan and a string.cpp
File metadata and controls
129 lines (127 loc) · 3.13 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//https://www.hackerrank.com/challenges/morgan-and-a-string/problem
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <memory.h>
using namespace std;
#define FILL(a, val) memset((a), (val), sizeof(a));
namespace SuffixArray
{
const int MAXSIZE = 200100;
const int ALPHABET = 128;
int p[MAXSIZE], c[MAXSIZE], cnt[MAXSIZE];
int pn[MAXSIZE], cn[MAXSIZE];
vector<int> getSuffixArray(string& s)
{
FILL(cnt, 0);
int n = s.size();
for (int i = 0; i < n; ++i)
{
++cnt[s[i]];
}
for (int i = 1; i < ALPHABET; ++i)
{
cnt[i] += cnt[i-1];
}
for (int i = 0; i < n; ++i)
{
p[--cnt[s[i]]] = i;
}
int count = 1;
c[p[0]] = count-1;
for (int i = 1; i < n; ++i)
{
if (s[p[i]] != s[p[i-1]])
++count;
c[p[i]] = count - 1;
}
for (int h = 0; (1<<h) < n; ++h)
{
for (int i = 0; i < n; ++i)
{
pn[i] = p[i] - (1<<h);
if (pn[i] < 0)
pn[i] += n;
}
FILL(cnt, 0);
for (int i = 0; i < n; ++i)
{
++cnt[c[i]];
}
for (int i = 1; i < count; ++i)
{
cnt[i] += cnt[i-1];
}
for (int i = n-1; i >= 0; --i)
{
p[--cnt[c[pn[i]]]] = pn[i];
}
count = 1;
cn[p[0]] = count-1;
for (int i = 1; i < n; ++i)
{
int pos1 = (p[i] + (1<<h))%n;
int pos2 = (p[i-1] + (1<<h))%n;
if (c[p[i]] != c[p[i-1]] || c[pos1] != c[pos2])
++count;
cn[p[i]] = count - 1;
}
for (int i = 0; i < n; ++i)
{
c[i] = cn[i];
}
}
vector<int> res;
res.reserve(n);
for (int i = 0; i < n; ++i)
{
res.push_back(c[i]);
}
return res;
}
}
string solve(string& a, string& b)
{
a.push_back('a');
b.push_back('b');
string s = a+b;
vector<int> suffixArray = SuffixArray::getSuffixArray(s);
string res = "";
int pos1=0, pos2=0;
while (true)
{
if (pos1 >= (a.size()-1) && pos2 >= (b.size()-1))
{
break;
}
if (pos1 >= (a.size()-1))
{
res += b[pos2++];
continue;
}
if (pos2 >= (b.size()-1))
{
res += a[pos1++];
continue;
}
if (suffixArray[pos1] < suffixArray[a.size() + pos2])
res += a[pos1++];
else
res += b[pos2++];
}
return res;
}
int main()
{
ios_base::sync_with_stdio(false);
int T;
cin >> T;
for (int t = 0; t < T; ++t)
{
string a,b;
cin >> a >> b;
cout << solve(a,b) << endl;
}
return 0;
}