-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path1138D.cpp
More file actions
101 lines (96 loc) · 1.71 KB
/
1138D.cpp
File metadata and controls
101 lines (96 loc) · 1.71 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
//21171_somesh || asomesh999
//AC
#include<bits/stdc++.h>
using namespace std;
#define fr(i,a,b) for(long long i=a;i<b;i++)
#define io ios::sync_with_stdio(false);cin.tie(NULL);
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
long int mod=1000000007;
vector<int> prefix_function(string s) {
int n = (int)s.length();
vector<int> pi(n);
for (int i = 1; i < n; i++) {
int j = pi[i-1];
while (j > 0 && s[i] != s[j])
j = pi[j-1];
if (s[i] == s[j])
j++;
pi[i] = j;
}
return pi;
}
int main()
{
io
#ifdef SOMU
clock_t startTime = clock();
freopen("input.txt","r",stdin);
#endif
string s, t;
cin >> s >> t;
if(t.size() > s.size()) {
cout << s;
return 0;
}
int zero = 0, one = 0;
fr(i, 0, s.size()) {
if(s[i] == '1') one++;
else zero++;
}
vi len = prefix_function(t);
// cout << len << '\n';
int i = 0;
fr(j, 0, t.size()) {
if(i == s.size()) break;
if(t[j] == '0') {
if(zero > 0) {
cout << 0;
zero--;
} else {
cout << 1;
one--;
}
} else {
if(one > 0) {
cout << 1;
one--;
} else {
cout << 0;
zero--;
}
}
i++;
}
while(s.size() > i) {
fr(j, len[len.size() - 1], t.size()) {
if(i == s.size()) break;
if(t[j] == '0') {
if(zero > 0) {
cout << 0;
zero--;
} else {
cout << 1;
one--;
}
} else {
if(one > 0) {
cout << 1;
one--;
} else {
cout << 0;
zero--;
}
}
i++;
}
}
#ifdef SOMU
cerr << endl <<setprecision(20)<< double( clock() - startTime ) / (double)CLOCKS_PER_SEC<< " seconds." << endl;
#endif
return 0;
}