-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathZOJ1004(AC).cpp
132 lines (124 loc) · 2.18 KB
/
ZOJ1004(AC).cpp
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
130
131
132
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
using namespace std;
vector<string> res;
map<string, int> mm;
string a, b;
int sa, sb;
char s[2000];
void dfs(int x, int y, int len, stack<char> st, bool bb)
{
while(y < sb){
if(!st.empty() && y < sb && st.top() == b[y]){
while(!st.empty() && y < sb && st.top() == b[y]){
if(x < sa){
st.push(a[x++]);
s[len++] = 'i';
s[len++] = ' ';
dfs(x, y, len, stack<char>(st), true);
--len;
--len;
--x;
st.pop();
}
st.pop();
s[len++] = 'o';
s[len++] = ' ';
++y;
}
}else if(x < sa && a[x] == b[y]){
while(x < sa && a[x] == b[y]){
st.push(a[x++]);
s[len++] = 'i';
s[len++] = ' ';
dfs(x, y, len, stack<char>(st), true);
--len;
--len;
--x;
st.pop();
++x;
++y;
s[len++] = 'i';
s[len++] = ' ';
s[len++] = 'o';
s[len++] = ' ';
}
}else if(x < sa){
st.push(a[x++]);
s[len++] = 'i';
s[len++] = ' ';
}else{
break;
}
bb = false;
}
if(x >= sa && y >= sb && st.empty())
{
s[len] = 0;
string str = string(s);
if(mm.find(s) == mm.end()){
res.push_back(str);
mm.insert(pair<string, int>(str, (int)res.size()));
}
}
}
int main()
{
int ct[26];
int i;
int tmp;
stack<char> sta;
while(cin >> a >> b){
res.clear();
memset(ct, 0, 26 * sizeof(int));
if(a.size() != b.size()){
cout << "[" << endl;
cout << "]" << endl;
continue;
}
tmp = a.size();
for(i = 0; i < tmp; ++i){
if(a[i] >= 'a' && a[i] <= 'z'){
++ct[a[i] - 'a'];
}
}
tmp = b.size();
for(i = 0; i < tmp; ++i){
if(b[i] >= 'a' && b[i] <= 'z'){
--ct[b[i] - 'a'];
}
}
for(i = 0; i < 26; ++i){
if(ct[i]){
break;
}
}
if(i < 26){
cout << "[" << endl;
cout << "]" << endl;
continue;
}
while(!sta.empty()){
sta.pop();
}
res.clear();
mm.clear();
sa = a.size();
sb = b.size();
dfs(0, 0, 0, sta, false);
sort(res.begin(), res.end());
cout << "[" << endl;
tmp = res.size();
for(i = 0; i < tmp; ++i){
cout << res[i] << endl;
}
cout << "]" << endl;
}
return 0;
}