[USACO4.3.4 Letter Game]

[关键字]:枚举

[题目大意]:给出每个字母的分数,

//================================================================================================================

[分析]:一开始想了半天hash也没想对,这题居然枚举就能做。虽然给出的字典很大,但是其实有用的词很少。只要在读入字典时判断一下是否是由给出的字符中的一些组成,然后之久枚举超找最大值和词组就行。

[代码]:

View Code
/*

ID:procedure2

PROB:lgame

LANG:C++

*/

#include<iostream>

#include<cstdio>

#include<cstdlib>

#include<cstring>

#include<algorithm>

using namespace std;



const int value[26]={2,5,4,4,1,6,5,5,1,7,6,3,5,2,3,5,7,2,1,2,4,6,6,7,5,7};



int tot,a[26],b[26],ans,sum;

char s[10],s1[20],s2[20][20],word[40000][10];



bool Cleck(char *s)

{

int len=strlen(s);

memcpy(b,a,sizeof(a));

for (int i=0;i<len;i++)

if (s[i]!=' ') b[s[i]-'a']--;

for (int i=0;i<26;i++)

if (b[i]<0) return 0;

return 1;

}



void Init()

{

freopen("lgame.in","r",stdin);

freopen("lgame.out","w",stdout);

scanf("%s",s);

for (int i=0;i<strlen(s);i++) a[s[i]-'a']++;

fclose(stdin);

freopen("lgame.dict","r",stdin);

while (1)

{

scanf("%s",s1);

//printf("%s\n",s1);

if (s1[0]=='.') break;

if (Cleck(s1)) strcpy(word[++tot],s1);

}

fclose(stdin);

//printf("%d\n",tot);

//for (int i=1;i<=tot;i++) printf("%s\n",word[i]);

}



int sorce(char *s)

{

int sum=0;

for (int i=0;i<strlen(s);i++)

if (s[i]!=' ') sum+=value[s[i]-'a'];

return sum;

}



bool cmp(char *a,char *b)

{

if (strcmp(a,b)<0) return 1; else return 0;

}



void Solve()

{

for (int i=1;i<=tot;i++)

{

int temp=sorce(word[i]);

if (temp==ans) strcpy(s2[++sum],word[i]);

if (temp>ans) ans=temp,strcpy(s2[sum=1],word[i]);

}

for (int i=1;i<tot;i++)

for (int j=i+1;j<=tot;j++)

{

strcpy(s1,word[i]);

strcat(s1," ");

strcat(s1,word[j]);

//printf("%s\n",s1);

if (!Cleck(s1)) continue;

int temp=sorce(s1);

if (temp==ans) strcpy(s2[++sum],s1);

if (temp>ans) ans=temp,strcpy(s2[sum=1],s1);

}

printf("%d\n",ans);

for (int i=1;i<sum;i++)

for (int j=i+1;j<=sum;j++)

if (strcmp(s2[i],s2[j])>0)

{

strcpy(s1,s2[i]);

strcpy(s2[i],s2[j]);

strcpy(s2[j],s1);

}

for (int i=1;i<=sum;i++) printf("%s\n",s2[i]);

}



int main()

{

Init();

Solve();

return 0;

}



你可能感兴趣的:(USACO)