第六章部分例题

 

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <set>
  4 #include <cstring>
  5 
  6 using namespace std;
  7 
  8 const int maxn=1000;
  9 int UG[maxn][maxn];
 10 int vis[maxn];
 11 char str[1024];
 12 int in[maxn];
 13 int out[maxn];
 14 set<int> s;
 15 unsigned dfs_num=0;
 16 
 17 void dfs(int u)
 18 {
 19     vis[u]=1;
 20     dfs_num++;
 21 
 22     for(int i=0;i<='z'-'a';i++)
 23         if(UG[u][i])
 24             if(!vis[i])
 25                 dfs(i);
 26 }
 27 
 28 int main()
 29 {
 30     s.clear();
 31     memset(UG,0,sizeof(UG));
 32     memset(vis,0,sizeof(vis));
 33     memset(in,0,sizeof(in));
 34     memset(out,0,sizeof(out));
 35 
 36     int n;
 37 
 38     scanf("%d",&n);
 39 
 40     while(n--)
 41     {
 42         memset(str,0,sizeof(str));
 43         scanf("%s",str);
 44 
 45         printf("----%s---\n",str);
 46 
 47 
 48         if(s.count(str[0]-'a')==0)
 49             s.insert(str[0]-'a');
 50         if(s.count(str[strlen(str)-1]-'a')==0)
 51             s.insert(str[strlen(str)-1]-'a');
 52 
 53         UG[str[0]-'a'][str[strlen(str)-1]-'a']++;
 54         UG[str[strlen(str)-1]-'a'][str[0]-'a']++;
 55 
 56         out[str[0]-'a']++;
 57         in[str[strlen(str)-1]-'a']++;
 58     }
 59 
 60     //find odd
 61     
 62     //if have odd out return it
 63     //if have odd in return it
 64     
 65     printf("%d\n",s.size());
 66 
 67     int num1=0;
 68     int num2=0;
 69 
 70     for(int i=0;i<='z'-'a';i++)
 71     {
 72         i==0? printf("in:%d",in[i]):printf(" %d",in[i]);
 73     }
 74 
 75     cout<<endl;
 76 
 77     for(int i=0;i<='z'-'a';i++)
 78         i==0? printf("out:%d",out[i]):printf(" %d",out[i]);
 79 
 80     cout<<endl;
 81 
 82 
 83     //计算多少个入度不等于出度
 84     for(int i=0;i<'z'-'a';i++)
 85         if(in[i]!=out[i]) num1++;
 86 
 87 
 88     //大于两个就有问题
 89     if(num1>2)
 90     {
 91         cout<<"NO"<<endl;
 92         return 0;
 93     }
 94 
 95 
 96     //有多少个入度不等于出度的0个?
 97     
 98     
 99     if(num1==0)
100     {
101         for(int i=0;i<'z'-'a';i++)
102         {
103             if(s.count(i))
104             {
105                 dfs(i);
106 
107                 if(s.size()==dfs_num)
108                 {
109                     cout<<"YES"<<endl;
110 
111                     return 0;
112                 }
113                 else
114                 {
115                     cout<<"NO"<<endl;
116 
117                     return 0;
118                 }
119             }
120         }
121     }
122 
123 
124     //大于两个,进一步判断是否满足条件
125     if(num1==2)
126     {
127         for(int i=0;i<'z'-'a';i++)
128             if(in[i]!=out[i])
129             {
130                 num2=num1;
131                 num1=i;
132             }
133 
134             printf("num1:%d\n",num1);
135             printf("num2:%d\n",num2);
136 
137             if((in[num1]==out[num1]+1 && in[num2]+1==out[num2]) || (in[num2]==out[num2]+1 && in[num1]+1==out[num1]))
138             {
139                 for(int i=0;i<'z'-'a';i++)
140                     if(s.count(i))
141                     {
142 
143                         //标记所有节点判断是否连通
144                         dfs(i);
145 
146                         if(s.size()==dfs_num) cout<<"YES"<<endl;
147                         else 
148                             cout<<"NO"<<endl;
149 
150                         return 0;
151                     }
152             }
153             else 
154                 cout<<"NO"<<endl;
155     }
156 
157     return 0;
158 }

 

posted @ 2017-08-10 19:18  lan126  阅读(114)  评论(0编辑  收藏  举报