hash

讲得不错:hash

hash

uva,188

怎么把字符串提取出来,注意一下就可以了。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 
 7 using namespace std;
 8 
 9 int W[15],n,C;
10 
11 void solve()
12 {
13     for(int i=0;i<n;i++)
14     {
15         for(int j=i+1;j<n;j++)
16         {
17             if((C/W[i])%n==(C/W[j])%n)
18             {
19                 C=min((C/W[i]+1)*W[i],(C/W[j]+1)*W[j]);
20                 solve();
21                 return;
22             }
23                
24         }
25     }
26 }
27 
28 int main()
29 {
30     char s[200];
31     while(gets(s))
32     {
33         n=0;
34         memset(W,0,sizeof(W));
35         
36         for(int i=0;i<=strlen(s);i++)
37         {
38             if(s[i]==' ' || s[i]=='')
39             {
40                 n++;
41                 while(s[i+1]==' ')
42                     i++;
43             }
44             else
45             {
46                 W[n]=(W[n]<<5)+s[i]-'a'+1;
47             }
48         }
49         sort(W,W+n);
50         C=W[0];
51         solve();
52         
53         puts(s);
54         printf("%d
",C);
55         puts("");
56     }
57     return 0;
58 }
View Code

uva,10282

 1 #include <iostream>
 2 #include <map>
 3 #include <cstdio>
 4 #include <map>
 5 #include <sstream>
 6 using namespace std;
 7 
 8 map<string,string> mp;
 9 
10 int main()
11 {
12     string s1,s2;
13     mp.clear();
14     getline(cin,s1);
15     while(s1!="")
16     {
17         stringstream ss(s1);
18         ss>>s1>>s2;
19         mp[s2]=s1;
20         getline(cin,s1);
21     }
22     
23     while(cin>>s1)
24     {
25         if(mp.find(s1)!=mp.end())
26             cout<<mp[s1]<<'
';
27         else
28             cout<<"eh
";
29     }
30     return 0;
31 }
View Code

uva,10422

 1 #include<stdio.h>
 2 #include<string.h>
 3 int a[5][5],move[8][2]={{-1,-2},{-1,2},{-2,1},{-2,-1},{2,1},{2,-1},{1,-2},{1,2}},
 4     b[5][5]={{1,1,1,1,1},{0,1,1,1,1},{0,0,2,1,1},{0,0,0,0,1},{0,0,0,0,0}},min;
 5 int dfs(int x,int y,int step,int way)
 6 {int i,j,X,Y,sum=0,t;
 7  if (step>=min) return 0;
 8  for (i=0;i<5;i++)
 9  for (j=0;j<5;j++)
10  if (a[i][j]!=b[i][j]) ++sum;
11  if (sum==0) {if (step<min) min=step; return 0;}
12  if ((sum+1)/2+step>=min) return 0; //SUM表示与目标状态不同的位置个数,则最少需要(移动一次最多2个位置与目标状态相同)sum/2+sum%2=(sum+1)/2步才能变为目标状态
13  for (i=0;i<8;i++)
14  {X=x+move[i][0];
15   Y=y+move[i][1];
16   if ((X>=0)&&(X<5)&&(Y>=0)&&(Y<5)&&(way+i!=7)) //初始化move时将相反跳法的下标和为7,这样就可以每次避免走回头路省略判重过程
17   {t=a[x][y];a[x][y]=a[X][Y];a[X][Y]=t;
18    dfs(X,Y,step+1,i);
19    t=a[x][y];a[x][y]=a[X][Y];a[X][Y]=t;
20   }
21  }
22 }
23 int main()
24 {int i,j,x,y,n;
25  char s[6];
26  scanf("%d
",&n);
27  while (n--)
28  {
29   for (i=0;i<5;i++)
30   {gets(s);
31    for (j=0;s[j]!='';j++)
32    if (s[j]==' ') {a[i][j]=2;x=i;y=j;}
33              else a[i][j]=s[j]-'0';
34   }
35   min=11;
36   dfs(x,y,0,8);
37   if (min==11) printf("Unsolvable in less than 11 move(s).
");
38   else printf("Solvable in %d move(s).
",min);
39  }
40  return 0;
41 }
View Code
 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 
 5 int a[5][5],move[8][2]={{-1,-2},{-1,2},{-2,1},{-2,-1},{2,1},{2,-1},{1,-2},{1,2}},
 6 b[5][5]={{1,1,1,1,1},{0,1,1,1,1},{0,0,2,1,1},{0,0,0,0,1},{0,0,0,0,0}},min;
 7 
 8 int dfs(int x,int y,int step,int way)
 9 {
10     int sum=0,i,j,X,Y,t;
11     
12     if(step>=min)
13         return 0;
14     
15     for(i=0;i<5;i++)
16         for(j=0;j<5;j++)
17             if(a[i][j]!=b[i][j])
18                 ++sum;
19     if(sum==0)
20     {
21         if(step<min)
22             min=step;
23         return 0;
24     }
25     
26     if((sum+1)/2+step>=min)
27         return 0;
28     for(i=0;i<8;i++)
29     {
30         X=x+move[i][0];
31         Y=y+move[i][1];
32         if((X>=0) && (x<5) && (Y>=0) && (Y<5) && (way+i!=7))
33         {
34             t=a[x][y];
35             a[x][y]=a[X][Y];
36             a[X][Y]=t;
37             
38             dfs(X,Y,step+1,i);
39             
40             t=a[x][y];
41             a[x][y]=a[X][Y];
42             a[X][Y]=t;
43         }
44     }
45     return 0;
46 }
47 
48 int main()
49 {
50     int n,x,y,i,j;
51     char s[6];
52     scanf("%d
",&n);
53     while(n--)
54     {
55         x=y=0;
56         for(i=0;i<5;i++)
57         {
58             gets(s);
59             for(j=0;s[j]!='';j++)
60             {
61                 if(s[j]==' ')
62                 {
63                     x=i;
64                     y=j;
65                     a[i][j]=2;
66                 }
67                 else
68                     a[i][j]=s[j]-'0';
69             }
70         }
71         min=11;
72         dfs(x,y,0,8);
73         
74         if(min==11)
75             printf("Unsolvable in less than 11 move(s).
");
76         else
77             printf("Solvable in %d move(s).
",min);
78     }
79     return 0;
80 }
有时候你离成功只差一步了。
活在现实,做在梦里。
原文地址:https://www.cnblogs.com/do-it-best/p/5428150.html