做了4个小时的大模拟,MLE了,又做了2个小时优化之后TLE了。。。实在不想说什么了
//Hierarchical Notation.cpp
#include
#define LL long long
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define PI 3.1415926535897932626
#define EXIT exit(0);
#define DEBUG puts("Here is a BUG");
#define CLEAR(name, init) memset(name, init, sizeof(name))
const double eps = 1e-8;
const int MAXN = (int)4e5 + 5;
using namespace std;
int res[1005];
char part[MAXN];
char notation[MAXN];
map
> pos;
void out(int x) {
int brackets_left = 0;
while(notation[x] && ((notation[x] != ',' && notation[x] != '}') || brackets_left)) {
brackets_left += notation[x] == '{';
brackets_left -= notation[x] == '}';
putchar(notation[x++]);
}
puts("");
}
void slove(void) {
int k = 0;
CLEAR(part, 0);
part[k] = '.';
for(int i = 0; notation[i]; i++) {
if (notation[i] == '{') {
while(k > 0 && part[k-1] != '|') part[k--] = '\0';
if (k) k--;
part[k++] = '.';
part[k] = '\0';
}
else if (notation[i] == '}') {
while(k > 0 && part[k-1] != '.') part[k--] = '\0';
part[k--] = '\0';
while(k > 0 && part[k-1] != '.') part[k--] = '\0';
part[k] = '\0';
}
else if (notation[i] == ':') {
if (pos.find(part) != pos.end()) {
for(int j = 0; j < pos[part].size(); j++) {
res[pos[part][j]] = i+1;
}
}
part[k++] = '|';
part[k] = '\0';
}
else if (notation[i] == ',') {
while(k > 0 && part[k-1] != '.') part[k--] = '\0';
}
else {
part[k++] = notation[i];
part[k] = '\0';
}
// puts(part);
}
}
int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
freopen("D:\\Documents\\Disk_Synchronous\\Programs\\Acm\\input.txt", "r", stdin);
#endif
int T; cin >> T;
while(T--) {
CLEAR(res, -1);
pos.clear();
cin >> notation;
int n; cin >> n;
for(int i = 0; i < n; i++) {
char tmp[MAXN]; tmp[0] = '.';
scanf("%s", tmp+1);
pos[tmp].push_back(i + 1);
}
slove();
for(int i = 1; i <= n; i++) {
if (res[i] == -1) puts("Error!");
else out(res[i]);
}
}
return 0;
}