您的位置:首页 > 其它

[USACO4.3.3]Letter Game

2017-08-08 20:02 274 查看
新博客请参见emoairx.github.io
Letter Game

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 file lgame.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


题意:http://www.nocow.cn/index.php/Translate:USACO/lgame

40000^2似乎要超?是不是??

是的。那么如何处理呢?

注意到:
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.

也就是给定的串最多只有7个字符,那么。

我们可以把读入的dict文件里面,含有a所不含有的直接踢掉!。

如何处理呢??对读入的字符串的每一位打标记。

(当然,可以把包含地多了的也踢掉,但是并

没有必要,代码会复杂很多) 

在结果的单词或词组构所成的字母中,每个字母出现的频数不大于输入的字符串中每个字母的频数。也就是例如输入是 aaa,字典中有单词 a,aa,aaa,aaaa,其中a,aa,aaa都是符合条件的解,但只有aaa才是要输出的最优解。

可能要字典序????,但是好像要先输出词组,然后输出单词就可以过了。 

其实还有另外一种枚举思路

枚举每一个位置,然后用Map搞一搞。代码可能简单很多。 

/*
ID:cqz15311
LANG:C++
PROG:lgame
*/
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int score[26],n;
void f(char a,int b){
score[a-'a'] = b;
}
void init(){
f('q',7);f('w',6);f('e',1);f('r',2);f('t',2);f('y',5);f('u',4);f('i',1);f('o',3);f('p',5);
f('a',2);f('s',1);f('d',4);f('f',6);f('g',5);f('h',5);f('j',7);f('k',6);f('l',3);
f('z',7);f('x',7);f('c',4);f('v',6);f('b',5);f('n',2);f('m',5);
}
char a[40005][20];

int flag[26];
bool check1(char s[]){
int Flag[26];
memset(Flag,0,sizeof(Flag));
for (int i=0;s[i];i++){
Flag[s[i]-'a']++;
}
for (int i=0;i<26;i++){
if (Flag[i] > flag[i]) return false;
}
return true;
}

bool check2(char s[],char t[]){
int Flag[26];
memset(Flag,0,sizeof(Flag));
for (int i=0;s[i];i++){
Flag[s[i]-'a']++;
}
for (int i=0;t[i];i++){
Flag[t[i]-'a']++;
}
for (int i=0;i<26;i++){
if (Flag[i] > flag[i]) return false;
}
return true;
}

int Score(char s[]){
int Ans = 0;
for (int i=0;s[i];i++){
Ans = Ans + score[s[i] - 'a'];
}
return Ans;
}

void solve(){
int Ans = 0;
int ans[40005][2];
int Num = 0;
memset(ans,0,sizeof(ans));
for (int i=0;i<n;i++){
for (int j=i+1;j<n;j++){
if (check2(a[i],a[j])){
int x = Score(a[i]) + Score(a[j]);
if (x > Ans){
Ans = x;
Num = 0;
ans[++Num][0] = i;
ans[Num][1] = j;
} else
if (x == Ans){
ans[++Num][0] = i;
ans[Num][1] = j;
}
}
}
}

for (int i=0;i<n;i++){
//		printf("%d\n",i);
if (check1(a[i])){
int x = Score(a[i]);
//			printf("%s %d\n",a[i],x);
if (x > Ans){
Ans = x;
Num = 0;
ans[++Num][0] = i;
ans[Num][1] = -1;
} else
if (x == Ans){
ans[++Num][0] = i;
ans[Num][1] = -1;
}
}
}
//考虑单独一个的
printf("%d\n",Ans);
//	printf("%d\n",Num);
for (int i=1;i<=Num;i++){
printf("%s",a[ans[i][0]]);
if (ans[i][1] != -1){
printf(" %s\n",a[ans[i][1]]);
} else puts("");
}
}

int main(){
init();
freopen("lgame.in","r",stdin);
char s[20];
scanf("%s",&s);
memset(flag,0,sizeof(flag));
for(int i=0;s[i];i++){
flag[s[i] - 'a']++;
//		printf("%c %d\n",s[i],flag[s[i]-'a']);
}
fclose(stdin);
freopen("lgame.dict","r",stdin);
n = 0;
while (scanf("%s",&a
) && a
[0]!='.'){
for (int i=0;a
[i];i++){
if (!flag[a
[i] - 'a']){
n--;
break;
}
}
n++;
}
freopen("lgame.out","w",stdout);
solve();
fclose(stdout);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: