当前位置:   article > 正文

C++——【USACO 4.3.3】——Letter Game_usaco letter game

usaco letter game

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

字母游戏
IOI 1995
图1:每个26个小写字母和它的值
字母游戏在家里和电视上都很流行。在游戏的一个版本中,每一个字母都有一个值,你可以收集字母来形成一个或多个单词以获得最高的分数。除非你有“一种方法”,否则你会尝试所有你知道的单词,有时查找拼写,然后计算分数。显然,这可以通过计算机更精确地完成。
给出图1中的值,单词列表和所收集的字母:找到可以形成的最高值的单词或词组。
项目名称:lgame
输入格式
一行有一系列小写字母(从“a”到“z”)。该字符串由至少3个字母组成,最多7个字母。
示例输入(文件lgame.in)
prmgroa
字典格式
在大多数4万行中,每个字符串包含至少3个字符串,最多7个小写字母。在这个文件的末尾,有一行(' . ')。文件按字母顺序排序,不包含重复项。
样本字典(文件lgame.dict)

profile
program
prom
rag
ram
rom
.
输出格式
在第一行,你的程序应该写最高可能的分数,在每一行,所有的单词和/或单词对从文件lgame.dict获得。按第一个字按字母顺序排列输出,如果被绑定,则按第二个字。在输出行中,字母 不能 多于输入的字母。使用图1中给出的字母值。

当两个单词的组合可以用给定的字母组成时,单词应该印在同一行上。这两个字应该按字母顺序排列;例如,不要写“rag prom”,只写“prom rag”。一个输出行中的一对可能包含两个相同的单词。
样例输出(文件lgame.out)
这个输出使用
上面的小字典,而不是lgame.dict。

24
program
prom rag

  1. /*
  2. ID : mcdonne1
  3. LANG : C++
  4. TASK : lgame
  5. */
  6. #pragma GCC optimize("O3")
  7. #include <iostream>
  8. #include <fstream>
  9. #include <cstring>
  10. using namespace std;
  11. string dic[40001];
  12. string ans[20];
  13. string s;
  14. int n, l, __m, temp, maxx, __ans;
  15. int hs[26], ps[26], qs[26];
  16. int len[40001], m[40001];
  17. const int val[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};
  18. inline void find(int x) {
  19. for (int i = x + 1; i < n ;i++)
  20. if (m[i] <= __m) {
  21. memcpy (qs, ps, sizeof(ps));
  22. for (int j =0; j < len[i]; j++)
  23. if (!qs[dic[i][j] - 'a']) goto __Con;
  24. else qs[dic[i][j] - 'a']--;
  25. if (m[i] + m[temp] > maxx) {
  26. maxx = m[i] + m[temp];
  27. __ans = 1;
  28. ans[1] = dic[temp] + " " + dic[i];
  29. } else if(m[i] + m[temp] == maxx)
  30. ans[++__ans] = dic[temp] + " " + dic[i];
  31. __Con : ;
  32. }
  33. }
  34. int main () {
  35. ios::sync_with_stdio(false);
  36. cin.tie(NULL);
  37. cout.tie(NULL);
  38. ifstream cin("lgame.dict", ios::in);
  39. while(cin>>dic[n]) n++;
  40. --n;
  41. for (int i = 0; i < n; i++) {
  42. len[i] = dic[i].length();
  43. for (int j = 0; j < len[i]; j++)
  44. m[i] += val[dic[i][j] - 'a'];
  45. }
  46. cin.close();
  47. ifstream fin("lgame.in", ios::in);
  48. ofstream fout("lgame.out", ios::out);
  49. fin>>s;
  50. l = s.length();
  51. for (int i = 0; i < l; i++) {
  52. __m += val[s[i] - 'a'];
  53. ++hs[s[i] - 'a'];
  54. }
  55. for (int i = 0; i < n; i++)
  56. if (len[i] <= l and m[i] <= __m) {
  57. memcpy (ps, hs, sizeof(hs));
  58. for (int j = 0; j < len[i]; j++)
  59. if (!ps[dic[i][j] - 'a']) goto Con;
  60. else ps[dic[i][j] - 'a']--;
  61. if (m[i] > maxx) {
  62. maxx = m[i];
  63. __ans = 1;
  64. ans[1] = dic[i];
  65. } else if(m[i] == maxx)
  66. ans[++__ans] = dic[i];
  67. if (l - len[i] >= 3) {
  68. temp = i;
  69. __m -= m[i];
  70. find(i);
  71. __m += m[i];
  72. }
  73. Con : ;
  74. }
  75. fout<<maxx<<endl;
  76. for (int i = 1; i<= __ans; i++)
  77. fout<<ans[i]<<endl;
  78. fin.close();
  79. fout.close();
  80. return 0;
  81. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/710491
推荐阅读
相关标签
  

闽ICP备14008679号