Letter Game_usaco4.3_枚举
2016-11-25 23:22
337 查看
Description
Figure 1: Each of the 26 lowercase letters and its valueLetter 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 (froma' toz’). The string consists of at least 3 and at most 7 letters in arbitrary order.
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..
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 writeprom rag’. A pair in an output line may consist of two identical words.
Analysis
表示不会用c++的字符数组,蛋疼先排除字典中不含给出单词字母的,然后O(n2)暴力枚举咯,最后找答案的时候记得要按字典序输出
Code
/* ID:wjp13241 PROG:lgame LANG:C++ */ #include <stdio.h> #include <string.h> #include <algorithm> #define N 40001 #define W 27 #define L 8 using namespace std; char t [L],wd[L],d [L],prt[N*10+1][L+2]; int v[W],w[W],p ,score[]={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 max(int x,int y){return x>y?x:y;} int main() { freopen("lgame.in","r",stdin); freopen("lgame.out","w",stdout); scanf("%s",wd); fclose(stdin); freopen("lgame.dict","r",stdin); for (int i=0;i<strlen(wd);i++) w[wd[i]-'a']++; char s[L]; int cnt=0; int ans=0; while (scanf("%s",s)&&s[0]!='.') { if (strlen(s)>7||strlen(s)<3) continue; for (int i=0;i<W;i++)v[i]=w[i]; int val=0; bool flag=false; for (int i=0;i<strlen(s);i++) if (!v[s[i]-'a']) { flag=true; break; } else v[s[i]-'a']--,val+=score[s[i]-'a']; if (!flag) { p[++cnt]=val; strcpy(d[cnt],s); ans=max(ans,p[cnt]); } } for (int i=1;i<=cnt;i++) for (int j=i+1;j<=cnt;j++) if (strlen(d[i])+strlen(d[j])<8) { char s[L]=""; for (int k=0;k<strlen(d[i]);k++) s[k]=d[i][k]; for (int k=0;k<strlen(d[j]);k++) s[k+strlen(d[i])]=d[j][k]; for (int k=0;k<W;k++)v[k]=w[k]; int val=0; bool flag=false; for (int k=0;k<strlen(s);k++) if (!v[s[k]-'a']) { flag=true; break; } else v[s[k]-'a']--,val+=score[s[k]-'a']; if (!flag) ans=max(ans,p[i]+p[j]); } printf("%d\n",ans); int ansCnt=0; for (int i=1;i<=cnt;i++) if (p[i]==ans) strcpy(prt[++ansCnt],d[i]); for (int i=1;i<=cnt;i++) for (int j=i+1;j<=cnt;j++) if (strlen(d[i])+strlen(d[j])<8) { char s[L]=""; for (int k=0;k<strlen(d[i]);k++) s[k]=d[i][k]; for (int k=0;k<strlen(d[j]);k++) s[k+strlen(d[i])]=d[j][k]; for (int k=0;k<W;k++)v[k]=w[k]; int val=0; bool flag=false; for (int k=0;k<strlen(s);k++) if (!v[s[k]-'a']) { flag=true; break; } else v[s[k]-'a']--,val+=score[s[k]-'a']; if (!flag) if (ans==p[i]+p[j]) { ++ansCnt; for (int k=0;k<strlen(d[i]);k++) prt[ansCnt][k]=d[i][k]; prt[ansCnt][strlen(d[i])]=' '; for (int k=0;k<strlen(d[j]);k++) prt[ansCnt][1+k+strlen(d[i])]=d[j][k]; } } for (int i=1;i<=ansCnt;i++) for (int j=i+1;j<=ansCnt;j++) if (strcmp(prt[i],prt[j])==1) { char tmp[L+2]=""; strcpy(tmp,prt[i]); strcpy(prt[i],prt[j]); strcpy(prt[j],tmp); } // sort(prt+1,prt+ansCnt+1); for (int i=1;i<=ansCnt;i++) printf("%s\n",prt[i]); return 0; }
相关文章推荐
- USACO Letter Game 解题报告
- usaco letter game
- USACO 4.3 The Primes(模拟枚举)
- usaco Letter Game
- USACO-Section 4.3 Letter Game (枚举)
- Letter Game[USACO]
- USACO Section 4.3 Letter Game - 简单枚举
- POJ 1753:Flip Game:棋盘枚举1
- poj Flip Game(DFS)(枚举)
- usaco training-A Game
- USACO-Section3.1 Contact【暴力枚举】
- Codeforces 327A Flipping Game(区间DP,暴力枚举)
- usaco 4.3 Street Race
- USACO / Feed Ratios (枚举||克莱姆法则||高斯消元)
- usaco 4.3 the primes 2010.8.6
- USACO Section 1.4 Packing Rectangles(枚举)
- bzoj 1647: [Usaco2007 Open]Fliptile 翻格子游戏(枚举)
- [BZOJ3404][Usaco2009 Open]Cow Digit Game又见数字游戏
- 【BZOJ】2102: [Usaco2010 Dec]The Trough Game(暴力)
- USACO 1.2 方块转换 (枚举)