HDU12191226(杭州电子科技大学第三届程序设计大赛+部分题解)

1219 水~

View Code
 1 #include<stdio.h>
 2 #include<string.h>
 3 const int maxn = 30;
 4 int a[ maxn ];
 5 char s[ 100005 ];
 6 int main(){
 7     while( gets( s )!=NULL ){
 8         memset( a,0,sizeof( a ) );
 9         int len=strlen( s );
10         for( int i=0;i<len;i++ ){
11             if( s[i]>='a'&&s[i]<='z' )
12                 a[ s[i]-'a' ]++;
13         }
14         for( int i=0;i<26;i++ ){
15             printf("%c:%d\n",i+'a',a[i]);
16         }
17         printf("\n");
18     }
19     return 0;
20 }

 1222

我是找规律的。。。对于n,m,如果他们之间有公共约数,则说明在狼走第二圈的时候,会走重复的点。

View Code
 1 #include<stdio.h>
 2 int gcd( int a,int b ){
 3     int r;
 4     while( b ){
 5         r=a%b;
 6         a=b;
 7         b=r;
 8     }
 9     return a;
10 }
11 int main(){
12     int t;
13     scanf("%d",&t);
14     while(t--){
15         int n,m;
16         scanf("%d%d",&m,&n);
17         //while( m>n )
18             //m-=n;
19         if( m==1||n==1 ){
20             printf("NO\n");
21             continue;
22         }
23         if( m==n ){
24             printf("YES\n");
25             continue;
26         }
27         if( gcd(n,m)!=1 ){
28             printf("YES\n");
29         }
30         else{
31             printf("NO\n");
32         }
33     }
34     return 0;
35 }            

 1224

SPFA  挺水的~~~

简单题吧

View Code
  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<stdlib.h>
  4 #include<algorithm>
  5 #include<queue>
  6 #include<stack>
  7 using namespace std;
  8 const int maxn = 105;
  9 const int maxm = 10000;
 10 const int inf = 99999999;
 11 int cnt,head[ maxn ];
 12 struct node{
 13     int u,val,next;
 14 }edge[ maxm ];
 15 int n,m;
 16 int val[ maxn ];
 17 int dis[  maxn ],vis[ maxn ],path[ maxn ];
 18 
 19 void init(){
 20     cnt=0;
 21     memset( head,-1,sizeof( head ));
 22 }
 23 
 24 void addedge( int a,int b,int c ){
 25     edge[ cnt ].u=b;
 26     edge[ cnt ].val=c;
 27     edge[ cnt ].next=head[ a ];
 28     head[ a ]=cnt++;
 29 }
 30 
 31 void bfs(){
 32     for( int i=1;i<=n+1;i++ ){
 33         vis[i]=0;
 34         dis[i]=-inf;
 35         path[i]=-1;
 36     }
 37     queue<int>q;
 38     while( !q.empty() )
 39         q.pop();
 40     q.push( 1 );
 41     vis[1]=1;
 42     dis[1]=0;
 43     while( !q.empty() ){
 44         int now=q.front();
 45         q.pop();
 46         vis[ now ]=0;
 47         for( int i=head[ now ];i!=-1;i=edge[ i ].next ){
 48             int next=edge[i].u;
 49             if( dis[next]<dis[now]+val[next] ){
 50                 dis[next]=dis[now]+val[next];
 51                 path[next]=now;
 52                 if( vis[next]==0 ){
 53                     vis[next]=1;
 54                     q.push(next);
 55                 }
 56             }
 57         }
 58     }
 59 }
 60 void output(){
 61     printf("points : %d\n",dis[n+1]);
 62     stack<int>s;
 63     for( int i=n+1;i!=-1;i=path[i] ){
 64         s.push( i );
 65     }
 66     printf("circuit : ");
 67     printf("%d",s.top());
 68     s.pop();
 69     while( !s.empty() ){
 70         if( s.top()==(n+1) )
 71             printf("->%d",1);
 72         else
 73             printf("->%d",s.top());
 74         s.pop();
 75     }
 76     printf("\n");
 77 }
 78 int main(){
 79     int T;
 80     scanf("%d",&T);
 81     for( int ca=1;ca<=T;ca++ ){
 82         if( ca!=1 )
 83             printf("\n");
 84         scanf("%d",&n);
 85         for( int i=1;i<=n;i++ )
 86             scanf("%d",&val[ i ]);
 87         val[n+1]=0;
 88         scanf("%d",&m);
 89         int a,b;
 90         init();
 91         while( m-- ){
 92             scanf("%d%d",&a,&b);
 93             if( a<b ){
 94                 addedge( a,b,0 );
 95             }
 96             else if( a>b ){
 97                 addedge( b,a,0 );
 98             }
 99         }
100         bfs();
101         printf("CASE %d#\n",ca);
102         //printf("%d\n",dis[n+1]);
103         output();
104     }
105     return 0;
106 }

 1225

字符串的水题~~~

View Code
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 using namespace std;
 5 const int maxn = 5005;
 6 struct node{
 7     char name[ 105 ];
 8     int win,lost,diff,score;
 9 }team[ maxn ];
10 int cnt;
11 void init( int n ){
12     for( int i=0;i<=n;i++ ){
13         team[ i ].win=team[ i ].lost=team[ i ].diff=team[ i ].score=0;
14         //memset( team[i].name,'\0',sizeof( team[i].name ));
15     }
16     cnt=0;
17 }
18 int find( char tt[] ){
19     if( cnt==0 ){
20         strcpy( team[0].name,tt );
21         cnt++;
22         return 0;
23     }
24     int i;
25     for( i=0;i<cnt;i++ ){
26         if( strcmp( tt,team[i].name )==0 ){
27             return i;
28         }
29     }
30     if( i==cnt ){
31         strcpy( team[cnt].name,tt );
32         cnt++;
33         return cnt-1;
34     }
35 }
36 bool cmp( node a,node b ){
37     if( a.score!=b.score )
38         return a.score>b.score;
39     else if( a.diff!=b.diff )
40         return a.diff>b.diff;
41     else if( a.win!=b.win )
42         return a.win>b.win;
43     else if( strcmp( a.name,b.name )>0 )
44         return false;
45     else return true;
46 }
47 int main(){
48     int n;
49     while( scanf("%d",&n)!=EOF ){
50         init( n );
51         char n1[ 105 ],n2[ 105 ];
52         int num1,num2;
53         for( int i=0;i<(n*(n-1));i++ ){
54             scanf("%s VS %s %d:%d",n1,n2,&num1,&num2);
55             int f1=find( n1 );
56             int f2=find( n2 );
57             if( num1==num2 ){
58                 team[ f1 ].score++;
59                 team[ f2 ].score++;
60                 team[ f1 ].win+=num1;
61                 team[ f1 ].lost+=num1;
62                 team[ f2 ].win+=num2;
63                 team[ f2 ].lost+=num2;
64             }
65             else if( num1>num2 ){
66                 team[ f1 ].win+=num1;
67                 team[ f1 ].lost+=num2;
68                 team[ f2 ].win+=num2;
69                 team[ f2 ].lost+=num1;
70                 team[ f1 ].score+=3;
71             }
72             else if( num1<num2 ){
73                 team[ f1 ].win+=num1;
74                 team[ f1 ].lost+=num2;
75                 team[ f2 ].win+=num2;
76                 team[ f2 ].lost+=num1;
77                 team[ f2 ].score+=3;
78             }
79         }
80         for( int i=0;i<n;i++ ){
81             team[ i ].diff=team[ i ].win-team[ i ].lost;
82         }
83         sort( team,team+n,cmp );
84         for( int i=0;i<n;i++ ){
85             printf("%s %d\n",team[i].name,team[i].score);
86         }
87         printf("\n");
88     }
89     return 0;
90 }

 1226

BFS+5000个状态+mod!!!

View Code
  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<string>
  4 #include<algorithm>
  5 #include<math.h>
  6 #include<iostream>
  7 #include<queue>
  8 using namespace std;
  9 const int maxn = 24;
 10 const int maxm = 5105;
 11 const int inf = 99999999;
 12 int n,m,c;
 13 int a[ maxn ];
 14 int vis[ maxm ];
 15 int flag;
 16 struct node{
 17     string ans;
 18     int mod;
 19 };
 20 string res;
 21 queue<node>q;
 22 void init2(){
 23     while( !q.empty() )
 24         q.pop();
 25 }
 26 void init1(){
 27     memset( a,0,sizeof( a ));
 28     memset( vis,0,sizeof( vis ));
 29     flag=-1;
 30     res.clear();
 31 }
 32 void bfs(){
 33     init2();
 34     node now,next;
 35     for( int i=1;i<16;i++ ){
 36         if( a[i] ){
 37             vis[ i%n ]=1;
 38             if( i>=0&&i<=9 ){
 39                 now.ans="";
 40                 now.ans=(i+'0');
 41                 now.mod=i%n;
 42                 q.push( now );
 43             }
 44             else{
 45                 now.ans="";
 46                 now.ans=(i+'A'-10);
 47                 now.mod=i%n;
 48                 q.push( now );
 49             }
 50         }
 51     }//the init
 52     while( !q.empty() ){
 53         now=q.front(),q.pop();
 54         if( flag==1&&now.ans.size()>res.size() )
 55             continue;
 56         if( now.mod==0 ){
 57             if( flag==-1 ){
 58                 flag=1;
 59                 res=now.ans;
 60             }
 61             else{
 62                 if( now.ans.size()<res.size() ){
 63                     res=now.ans;
 64                 }
 65                 else if( now.ans.size()==res.size()&&res>now.ans ){
 66                     res=now.ans;
 67                 }
 68             }
 69         }
 70         for( int i=0;i<16;i++ ){
 71             if( a[i] ){
 72                 if( i<10 ){
 73                     next=now;
 74                     next.ans+=( i+'0' );
 75                     next.mod=(now.mod*c+i)%n;
 76                     if( ( next.ans.size()<=500&&vis[ next.mod ]==0 )||( next.mod==0 ) ){
 77                         vis[ next.mod ]=1;
 78                         q.push( next );
 79                     }
 80                 }
 81                 else{
 82                     next=now;
 83                     next.ans+=(i+'A'-10);
 84                     next.mod=(now.mod*c+i)%n;
 85                     if( ( next.ans.size()<=500&&vis[ next.mod ]==0 )||( next.mod==0 ) ){
 86                         vis[ next.mod ]=1;
 87                         q.push( next );
 88                     }
 89                 }
 90             }
 91         }
 92     }
 93 }
 94 
 95 int main(){
 96     int ca;
 97     scanf("%d",&ca);
 98     while( ca-- ){
 99         scanf("%d%d%d",&n,&c,&m);
100         init1();
101         char ch[ 4 ];
102         for( int i=0;i<m;i++ ){
103             scanf("%s",ch);
104             if( ch[0]>='0'&&ch[0]<='9' )
105                 a[ ch[0]-'0' ]=1;
106             else
107                 a[ ch[0]-'A'+10 ]=1;
108         }
109          if(n==0){  
110            // cout<<0<<endl;  
111             if(a[0]==1)  
112                 cout<<0<<endl;  
113             else  
114                 cout<<"give me the bomb please"<<endl;  
115             continue;  
116         }  
117         bfs();
118         if( flag==-1 )
119             printf("give me the bomb please\n");
120         else
121             cout<<res<<endl;
122     }
123     return 0;
124 }
keep moving...
原文地址:https://www.cnblogs.com/xxx0624/p/2934021.html