标签:des style blog http color io os ar for
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2292 | Accepted: 878 |
Description
T ::= "(" N S ")"
S ::= " " T S
| empty
N ::= number
Input
Output
Sample Input
(2 (6 (7)) (3) (5 (1) (4)) (8)) (1 (2 (3))) (6 (1 (4)) (2 (3) (5)))
Sample Output
5 2 5 2 6 2 8 2 3 2 1 6 2 6
难点是 字符串的分离, (A)根据这个特点,当读到‘(‘时,下一个一定是数字,我们遇到’)‘就结束。见代码
1 #include"iostream" 2 #include"cstdio" 3 #include"map" 4 #include"queue" 5 #include"vector" 6 #include"set" 7 #include"cstring" 8 #include"algorithm" 9 using namespace std; 10 void solve(vector< set<int> > &v,int p=0) 11 { 12 int x; 13 cin>>x; 14 if(p) 15 { 16 v[x].insert(p); 17 v[p].insert(x); 18 } 19 while(1) 20 { 21 char ch; 22 cin>>ch; 23 if(ch==‘)‘) 24 break; 25 solve(v,x); 26 } 27 return ; 28 } 29 int main() 30 { 31 int i,j,n,k; 32 char ch; 33 while(cin>>ch) 34 { 35 vector< set<int> > vec(1024,set<int>()); 36 priority_queue<int,vector<int>,greater<int> > que; 37 n=0; 38 solve(vec); 39 for(i=1;i<vec.size();i++) 40 { 41 if(vec[i].size()) 42 { 43 n++; 44 if(vec[i].size()==1) 45 que.push(i); 46 } 47 } 48 for(i=1;i<n;i++) 49 { 50 int t=que.top(); 51 que.pop(); 52 int p=*vec[t].begin(); 53 if(i>1) 54 printf(" "); 55 printf("%d",p); 56 vec[p].erase(t); 57 if(vec[p].size()==1) 58 que.push(p); 59 } 60 printf("\n"); 61 } 62 return 0; 63 }
标签:des style blog http color io os ar for
原文地址:http://www.cnblogs.com/767355675hutaishi/p/4007059.html