zoj3826 DFS

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5380

#include<iostream>
#include<algorithm>
#include<string>
#include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};
#include<set>//int gcd(int a,int b){return b?gcd(b,a%b):a;}
#include<vector>
#include<cmath>
#include<queue>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define mod 1e9+7
#define ll long long
using namespace std;
map<ll,pair<int,int> > r;
string x[1001];
string y;
int w;
int idx(char c){
	if(c>='0'&&c<='9')
		return c-'0';
	else if(c>='A'&&c<='Z')
		return c-'A'+10;
	else if(c=='a'&&c<='z')
		return c-'a'+36;
	else
		return 62;
}
void dfs(ll p){
	ll tmp=p;
	while(y[w]!='}'){
		w++;
		if(y[w]=='}')
			return;
		p=tmp;
		while(y[w]!=':'){
			p=p*60+idx(y[w]);
			w++;
		}
		w++;
		int l=w;
		if(y[w]=='{')
			dfs(p*60+62);
		else{
			while(y[w+1]!='}'&&y[w+1]!=',')
				w++;
		}
		r[p]=make_pair(l,w);
		w++;
	}
}
int main(){
	int t;
	cin>>t;
	while(t--){
		r.clear();
		w=0;
		cin>>y;
		dfs(0);
		int n;
		cin>>n;
		for(int i=0;i<n;++i){
			cin>>x[i];
			ll s=0;
			for(int j=0;j<x[i].length();++j)
				s=s*60+idx(x[i][j]);
			if(r.count(s)){
				for(int j=r[s].first;j<=r[s].second;++j)
					cout<<y[j];
				cout<<endl;
			}
			else
				cout<<"Error!"<<endl;
		}
	}
	return 0;
}


你可能感兴趣的:(zoj3826 DFS)