UVa 122 Trees on the level

Code:
 1  #include<stdio.h>
 2  #include<stdlib.h>
 3  #include<iostream>
 4  #include<algorithm>
 5  #include<map>
 6  #include<vector>
 7  using namespace std;
 8  typedef pair<string,int> si;
 9  bool cmp(si a,si b)
10  {
11      if(a.first.length()<b.first.length()) return true;
12      else if(a.first.length()==b.first.length()) return a<b;
13      return false;
14  }
15  int main()
16  {
17      string start;
18      while(cin>>start)
19      {
20          bool flag=true;
21          vector<si> nodes;
22          map<string,bool> check;
23 
24          if(start[0]=='(' && start[1]==')')
25          {
26              printf("not complete\n");
27              continue;
28          }
29          auto it=find(start.begin(),start.end(),',');
30          int ind=it-start.begin();
31          string sub1,sub2;
32          sub1=start.substr(1,ind-1);
33          sub2=start.substr(ind+1,start.length()-ind-2);
34          int num=atoi(sub1.c_str());
35          nodes.push_back(si(sub2,num));
36          check[sub2]=true;
37          string str;
38          while(cin>>str)
39          {
40              if(str[0]=='(' && str[1]==')'break;
41 
42              auto it=find(str.begin(),str.end(),',');
43              int ind=it-str.begin();
44              string sub1,sub2;
45              sub1=str.substr(1,ind-1);
46              sub2=str.substr(ind+1,str.length()-ind-2);
47              int num=atoi(sub1.c_str());
48              nodes.push_back(si(sub2,num));
49              if(check[sub2]==true)
50              {
51                  flag=false;
52              }
53              else check[sub2]=true;
54          }
55          int len=nodes.size();
56          if(flag==true)
57          {
58              sort(nodes.begin(),nodes.end(),cmp);
59 
60              for(int i=0; i<len; i++)
61              {
62                  string tmp=nodes[i].first;
63                  if(check[tmp.substr(0,tmp.length()-1)]==false)
64                  {
65                      flag=false;
66                      break;
67                  }
68              }
69          }
70          if(flag==false)
71          {
72              printf("not complete\n");
73          }
74          else
75          {
76              for(int i=0; i<len; i++)
77              {
78                  printf("%d",nodes[i].second);
79                  if(i!=len-1) printf(" ");
80              }
81              printf("\n");
82          }
83      }
84      return 0;

85  }

留言

這個網誌中的熱門文章

Things a Little Bird Told Me: Confessions of the Creative Mind

UVa 12970 Alcoholic Pilots

UVa 483 Word Scramble