这个用类似词法分析器去模拟就可以了,对于路径,用一个栈去维护
代码:
#include <cstdio>
#include <cstring>
#include <map>
#include <string>
#include <algorithm>
#include <vector>
#include <iostream>
#include <stack>
using namespace std;
const int N = 1000005;
int t, n, u, hn;
char str[N], name[15], nn;
vector<pair<int, int> > ans;
vector<string> anss;
stack<int> S;
int gettoken() {
if (str[u] == '(') return 1;
else if (str[u] == ')') return 2;
else if (str[u] == ',') return 3;
else {
nn = 0;
while (str[u] >= 'a' && str[u] <= 'z')
name[nn++] = str[u++];
name[nn] = '\0';
return 4;
}
}
void gao() {
int tp = gettoken();
if (tp == 4) {
int tmp = hn++;
if (!S.empty()) ans.push_back(make_pair(S.top(), tmp));
S.push(tmp);
anss.push_back(name);
tp = gettoken();
if (tp != 1) {
S.pop();
if (!S.empty()) ans.push_back(make_pair(tmp, S.top()));
return;
}
else {
u++;
while (tp != 2) {
gao();
tp = gettoken();
u++;
}
S.pop();
if (!S.empty()) ans.push_back(make_pair(tmp, S.top()));
}
}
}
int main() {
scanf("%d", &t);
while (t--) {
hn = 1;
u = 0;
ans.clear();
anss.clear();
scanf("%s", str);
n = strlen(str);
gao();
cout << anss.size() << endl;
for (int i = 0; i < anss.size(); i++) cout << anss[i] << endl;
for (int i = 0; i < ans.size(); i++)
printf("%d %d\n", ans[i].first, ans[i].second);
printf("\n");
}
return 0;
}