Letter Game
IOI 1995
Figure 1: Each of the 26 lowercase lettersand its value Letter games are popular at home and on television. In oneversion of the game, every letter has a value, and you collect letters to formone or more words giving the highest possible score. Unless you have `a waywith words', you will try all the words you know, sometimes looking up thespelling, and then compute the scores. Obviously, this can be done moreaccurately by computer.
Given the values in Figure 1, a list ofwords, and the letters collected: find the highest scoring words or pairs ofwords 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 inarbitrary order.
SAMPLE INPUT (file lgame.in)
prmgroa
DICTIONARY FORMAT
At most 40,000 lines, each containing astring of at least 3 and at most 7 lowercase letters. At the end of this fileis a line with a single period (`.'). The file is sorted alphabetically andcontains no duplicates.
SAMPLE DICTIONARY (file lgame.dict)
profile
program
prom
rag
ram
rom
.
OUTPUT FORMAT
On the first line, your program shouldwrite the highest possible score, and on each of the following lines, all thewords and/or word pairs from file lgame.dict with this score. Sort the outputalphabetically by first word, and if tied, by second word. A letter must notoccur more often in an output line than in the input line. Use the lettervalues given in Figure 1.
When a combination of two words can beformed with the given letters, the words should be printed on the same lineseparated by a space. The two words should be in alphabetical order; forexample, do not write `rag prom', only write `prom rag'. A pair in an outputline 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
WA因:
1.各种PE,忘记输出最大值;
2.输出顺序的问题啊,最后要把答案sort一下
收获:
1.string的用法
2.sort里cmp的加强~~
// 只用一次DFS
/*
NAME:
PROG: lgame
LANG: C++
*/
#include <cstdio>
#include <string>
#include <fstream>
#include <cstring>
#include <algorithm>
using namespace std;
ifstream cin1("lgame.in");
ifstream cin2("lgame.dict");
ofstream cout("lgame.out");
int key[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};
string temp;
int lans=0,ldic,maxit=0;
struct node0
{
int w[26];
} stand,num;
struct node2
{
string s;
struct node0 k;
} dic[40001];
struct node3
{
string s,com;
} ans[40001];
void init()
{
cin1>>temp;
memset(stand.w,0,sizeof(stand.w));
for(int i=0;i<temp.size();i++)
stand.w[temp[i]-'a']++;
while (cin2>>temp,temp[0]!='.')
{
memset(num.w,0,sizeof(num.w));
bool flag=true;
for(int i=0;i<temp.size();i++)
{
int mid=temp[i]-'a';
num.w[mid]++;
if (num.w[mid]>stand.w[mid])
{
flag=false;
break;
}
}
if (flag)
{
ldic++;
dic[ldic].s=temp;
dic[ldic].k=num;
}
}
}
int min(int x,int y)
{
if (x<y)
return x;
else
return y;
}
bool cmp(node3 x,node3 y)
{
for(int i=0;i<min(x.com.size(),y.com.size());i++)
if (x.com[i]==y.com[i])
continue;
else
return (x.com[i]<y.com[i]);
}
void doit()
{
// sort(dic+1,dic+1+ldic,cmp);
//for(int i=1;i<=ldic;i++)
//cout<<dic[i].s<<endl;
memset(dic[0].k.w,0,sizeof(dic[0].k.w));
dic[0].s="";
for(int i=0;i<=ldic;i++)
for(int j=i+1;j<=ldic;j++)
{
bool flag=true;
int add=0;
for(int p=0;p<26;p++)
{
int he=dic[i].k.w[p]+dic[j].k.w[p];
if (he>stand.w[p])
{
flag=false;
break;
}
add+=he*key[p];
}
if (flag)
{
if (add==maxit)
{
lans++;
ans[lans].com=dic[i].s+dic[j].s;
if (i==0)
ans[lans].s=dic[j].s;
else
ans[lans].s=dic[i].s+' '+dic[j].s;
}
if (add>maxit)
{
maxit=add;
lans=1;
ans[lans].com=dic[i].s+dic[j].s;
if (i==0)
ans[lans].s=dic[j].s;
else
ans[lans].s=dic[i].s+' '+dic[j].s;
}
}
}
}
void outit()
{
cout<<maxit<<endl;
sort(ans+1,ans+lans+1,cmp);
for(int i=1;i<=lans;i++)
cout<<ans[i].s<<endl;
}
int main()
{
init();
doit();
outit();
return 0;
}
Compiling...
Compile: OK
Executing...
Test 1: TEST OK [0.043 secs, 7560 KB]
Test 2: TEST OK [0.043 secs, 7560 KB]
Test 3: TEST OK [0.043 secs, 7560 KB]
Test 4: TEST OK [0.032 secs, 7560 KB]
Test 5: TEST OK [0.043 secs, 7560 KB]
Test 6: TEST OK [0.043 secs, 7560 KB]
Test 7: TEST OK [0.043 secs, 7560 KB]
Test 8: TEST OK [0.054 secs, 7560 KB]
Test 9: TEST OK [0.054 secs, 7560 KB]
Test 10: TEST OK [0.043 secs, 7560 KB]
Test 11: TEST OK [0.043 secs, 7560 KB]
Test 12: TEST OK [0.032 secs, 7560 KB]
All tests OK.
Your program ('lgame') produced all correctanswers! This is your
submission #6 for this problem.Congratulations!
Here are the test data inputs:
------- test 1 -------
jicuzza
------- test 2 -------
cuqak
------- test 3 -------
pofax
------- test 4 -------
rammoxy
------- test 5 -------
culatu
------- test 6 -------
gvutvac
------- test 7 -------
bhruthr
------- test 8 -------
wagje
------- test 9 -------
thryst
------- test 10 -------
fnupfig
------- test 11 -------
bobdead
------- test 12 -------
abcdefg
Keep up the good work!