hdu 4041 2011北京赛区网络赛B 搜索 ***

时间:2024-10-25 21:04:32

直接在字符串上搜索,注意逗号的处理

 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
using namespace std;
#define MOD 1000000007
#define pb(a) push_back(a)
const int INF=0x3f3f3f3f;
const double eps=1e-;
typedef long long ll;
#define cl(a) memset(a,0,sizeof(a))
#define ts printf("*****\n");
const int MAXN=;
int n,m,tt,cnt;
int pos=,num=,len;
vector<int> vc;
vector<string> ans;
char s[];
void dfs()
{
if(pos>=len) return;
string ss;
while(s[pos]>='a'&&s[pos]<='z')
{
ss.push_back(s[pos]);
pos++;
}
ans.push_back(ss);
int now=num++;
vc.push_back(now);
if(s[pos]=='(')
{
pos++;
dfs();
vc.push_back(now);
while(s[pos]==',')
{
pos++;
dfs();
vc.push_back(now);
}
pos++;
}
else return;
}
int main()
{
int i,j,k;
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
#endif
scanf("%d",&tt);
while(tt--)
{
ans.clear();
vc.clear();
pos=;
num=;
scanf("%s",s);
len=strlen(s); dfs();
printf("%d\n",num-);
for(int i=;i<ans.size();i++){
printf("%s\n",ans[i].c_str());
}
for(int i=;i<vc.size()-;i++){
printf("%d %d\n",vc[i],vc[i+]);
}
printf("\n");
}
}