IOI 1995
Figure 1: Each of the 26 lowercase letters and its value
Letter games are popular at home and on television. In one version of the game, every letter has a value, and you collect letters to form one or more words giving the highest possible score. Unless you have `a way with words', you will try all the words you know, sometimes looking up the spelling, and then compute the scores. Obviously, this can be done more accurately by computer.
Given the values in Figure 1, a list of words, and the letters collected: find the highest scoring words or pairs of words that can be formed.
PROGRAM NAME: lgame
INPUT FORMAT
One line with a string of lowercase letters (from ` a' to ` z'). The string consists of at least 3 and at most 7 letters in arbitrary order.SAMPLE INPUT (file lgame.in)
prmgroa
DICTIONARY FORMAT
At most 40,000 lines, each containing a string of at least 3 and at most 7 lowercase letters. At the end of this file is a line with a single period (` .'). The file is sorted alphabetically and contains no duplicates.SAMPLE DICTIONARY (file lgame.dict)
profile program prom rag ram rom .
OUTPUT FORMAT
On the first line, your program should write the highest possible score, and on each of the following lines, all the words and/or word pairs from filelgame.dict with this score. Sort the output alphabetically by first word, and if tied, by second word. A letter must not occur more often in an output line than in the input line. Use the letter values given in Figure 1.
When a combination of two words can be formed with the given letters, the words should be printed on the same line separated by a space. The two words should be in alphabetical order; for example, do not write `rag prom', only write `prom rag'. A pair in an output line may consist of two identical words.
SAMPLE OUTPUT (file lgame.out)
This output uses the tiny dictionary above, not the lgame.dict dictionary.
24 program prom rag
/*
ID: conicoc1
LANG: C
TASK: lgame
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
FILE *fin,*fout,*flist;
int Score[123];
int Flag[123],TmpFlag[123];
char Goal[8];
char Word[100][8];
int WordC=0;
int Value[100];
int MaxValue=0;
char Ans[100][40];
int AnsC=0;
void Initial()
{
char c,Temp[8];
int i,sum;
Score['q']=7,Score['w']=6,Score['e']=1,Score['r']=2,Score['t']=2,Score['y']=5;
Score['u']=4,Score['i']=1,Score['o']=3,Score['p']=5;
Score['a']=2,Score['s']=1,Score['d']=4,Score['f']=6,Score['g']=5,Score['h']=5;
Score['j']=7,Score['k']=6,Score['l']=3;
Score['z']=7,Score['x']=7,Score['c']=4,Score['v']=6,Score['b']=5,Score['n']=2;
Score['m']=5;
memset(Flag,0,sizeof(Flag));
fscanf(fin,"%s",Goal);
for(i=0;Goal[i]!='\0';i++)
Flag[Goal[i]]++;
for(i=0;i<123;i++)
TmpFlag[i]=Flag[i];
fscanf(flist,"%s",Temp);
while(Temp[0]!='.'){
for(i=0;i<123;i++)
Flag[i]=TmpFlag[i];
for(i=0,sum=0;Temp[i]!='\0';i++)
if(!Flag[Temp[i]])
break;
else{
sum+=Score[Temp[i]];
Flag[Temp[i]]--;
}
if(Temp[i]=='\0'){
Value[WordC]=sum;
strcpy(*(Word+WordC++),Temp);
}
fscanf(flist,"%s",Temp);
}
}
void CountValue1(int i)
{
if(Value[i]>MaxValue){
MaxValue=Value[i];
AnsC=0;
strcpy(*(Ans+AnsC++),*(Word+i));
}
else if(Value[i]==MaxValue)
strcpy(*(Ans+AnsC++),*(Word+i));
}
void CountValue2(int i,int j)
{
int k,t,lenth;
if(Value[i]+Value[j]<MaxValue)
return;
for(k=0;k<123;k++)
Flag[k]=TmpFlag[k];
for(k=0;Word[i][k]!='\0';k++){
Flag[Word[i][k]]--;
if(Flag[Word[i][k]]<0)
return;
}
for(k=0;Word[j][k]!='\0';k++){
Flag[Word[j][k]]--;
if(Flag[Word[j][k]]<0)
return;
}
if(Value[i]+Value[j]>MaxValue){
MaxValue=Value[i]+Value[j];
AnsC=0;
}
t=0;
for(k=0;Word[i][k]!='\0';k++)
Ans[AnsC][t++]=Word[i][k];
Ans[AnsC][t++]=' ';
for(k=0;Word[j][k]!='\0';k++)
Ans[AnsC][t++]=Word[j][k];
Ans[AnsC++][t]='\0';
}
int main()
{
int i,j;
flist=fopen("lgame.dict","r");
fin=fopen("lgame.in","r");
fout=fopen("lgame.out","w");
Initial();
for(i=0;i<WordC;i++){
CountValue1(i);
for(j=i;j<WordC;j++)
CountValue2(i,j);
}
fprintf(fout,"%d\n",MaxValue);
for(i=0;i<AnsC;i++)
fprintf(fout,"%s\n",*(Ans+i));
return 0;
}
字符串的题目每次都是搞来搞去就过了。。莫名其妙