标签:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2350 | Accepted: 906 |
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
AC:直接暴力枚举所有情况,每次都从从节点个数最小的为1的开始;
1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cstring> 5 #include<queue> 6 #include<string> 7 #include<cmath> 8 using namespace std; 9 char ss[1000]; 10 int a[100][100],num[100]; 11 int ans[100]; 12 int main() 13 { 14 int len; 15 while(gets(ss)) 16 { 17 memset(a,0,sizeof(a)); 18 memset(ans,0,sizeof(ans)); 19 memset(num,0,sizeof(num)); 20 len = strlen(ss); 21 int aa = 0,i=0,lev=0,t=0,mmax =0,kk=0; 22 for(i = 0; i < len; i++) 23 { 24 if(ss[i] == ‘(‘) 25 lev++; 26 else if(ss[i] == ‘)‘) 27 lev--; 28 if(ss[i] >= ‘0‘ && ss[i] <= ‘9‘) 29 { 30 t = t * 10 + ss[i] - 48; 31 if(t > mmax) 32 mmax = t; 33 if(!(ss[i + 1] >= ‘0‘ && ss[i + 1] <= ‘9‘)) 34 { 35 num[lev] = t; 36 a[num[lev - 1]][num[lev]] = 1; 37 a[num[lev]][num[lev - 1]] = 1; 38 } 39 } 40 else 41 { 42 t = 0; 43 } 44 } 45 int sum,j,k,m; 46 for( i=1;i<=mmax;i++) 47 { 48 for( j=1;j<=mmax;j++) 49 { sum = 0; 50 for(k=1;k<=mmax;k++)//统计和他相连的数的个数 51 { 52 sum = sum+a[j][k]; 53 } 54 if(sum == 1) 55 { 56 for(m = 1;m<=mmax; m++) 57 if(a[j][m] == 1) 58 { 59 ans[kk++] = m; 60 a[j][m] = 0; 61 a[m][j] = 0; 62 j = 0 ; 63 break; 64 } 65 } 66 } 67 } 68 for(int i=0; i<kk; i++) 69 if(i == 0)printf("%d",ans[i]); 70 else printf(" %d",ans[i]); 71 printf("\n"); 72 } 73 return 0; 74 }
poj 2567 Code the Tree 河南第七届省赛
标签:
原文地址:http://www.cnblogs.com/lovychen/p/4479550.html