usaco 4.3.4 letter game

久违的 first time ac!
USER: sendong li [lisendo1]
  
  
LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.043 secs, 4132 KB] Test 2: TEST OK [0.043 secs, 4132 KB] Test 3: TEST OK [0.054 secs, 4132 KB] Test 4: TEST OK [0.054 secs, 4132 KB] Test 5: TEST OK [0.054 secs, 4132 KB] Test 6: TEST OK [0.043 secs, 4132 KB] Test 7: TEST OK [0.043 secs, 4132 KB] Test 8: TEST OK [0.043 secs, 4132 KB] Test 9: TEST OK [0.043 secs, 4132 KB] Test 10: TEST OK [0.043 secs, 4132 KB] Test 11: TEST OK [0.054 secs, 4132 KB] Test 12: TEST OK [0.054 secs, 4132 KB] All tests OK.

YOUR PROGRAM ('lgame') WORKED FIRST TIME! That's fantastic -- and a rare thing. Please accept these special automated 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!
 

Thanks for your submission!

Letter GameIOI 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



分析: 该题目的字典文件有40000行,乍一看以为 n^2的穷举算法会超时。但仔细一想,input中只有最多7位数字,因此排列组合最多情况也就只有 7! 种情况 枚举是完全可以通过的!


代码如下:

/*
ID: lisendo1
PROG: lgame
LANG: C++
*/
#include
  
  
   
   
#include
   
   
    
    
#include
    
    
     
     
#include
     
     
      
      
#include
      
      
        #include 
       
         #include 
        
          #include 
         
           #include 
          
            #include 
           
             #include 
            
              #include 
             
               #include 
               #include 
               
                 #define MAXN 200 #define nil -1 #define oo 100000000.0 using namespace std; ifstream in("lgame.in"); ofstream out("lgame.out"); ifstream dictin("lgame.dict"); int lnum[30] = {0}; string s[40010]; int value[40010] = {0}; int l_v[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}; int snum = 0; struct result { int a, b; }; int result_num = 0; result r[40010]; int cmp(result x, result y) { for (int i = 0, j = 0; i < s[x.a].size(), j < s[y.a].size(); i++, j++) { if (s[x.a][i] < s[y.a][j]) return 1; else if (s[x.a][i] > s[y.a][j]) return 0; } return s[x.a].size() < s[y.a].size(); } int main() { char ch; while (in >> ch) { if (ch >= 'a' && ch <= 'z') { lnum[ch-'a']++; } else break; } string str; int i, j; while (dictin >> str) { if (str[0] == '.') break; int flag = 1; int count[30] = {0}; for (i = 0; i < str.size(); i++) { if (lnum[str[i]-'a'] == 0) {flag = 0; break;} count[str[i]-'a']++; if (count[str[i] - 'a'] > lnum[str[i] - 'a']) { flag = 0; break; } } if (flag == 1) { s[snum] = str; for (i = 0; i < str.size(); i++) { value[snum] += l_v[str[i]-'a']; } snum++; //cout << str << endl; } } int max = 0; for (i = 0; i < snum; i++) { if (value[i] > max) { max = value[i]; result_num = 0; r[result_num].a = i; r[result_num].b = -1; result_num++; } else if (value[i] == max) { r[result_num].a = i; r[result_num].b = -1; result_num++; } } int m, n; cout << snum << endl; for (i = 0; i < snum; i++) { for (j = i; j < snum; j++) { int count[30] = {0}; for (m = 0; m < s[i].size(); m++) { count[s[i][m]-'a'] += 1; } for (n = 0; n < s[j].size(); n++) { count[s[j][n] - 'a'] += 1; if (count[s[j][n] - 'a'] > lnum[s[j][n]-'a']) { break; } } if (n == s[j].size()) { if (value[i] + value[j] > max) { max = value[i] + value[j]; result_num = 0; r[result_num].a = i; r[result_num].b = j; result_num++; } else if (value[i] + value[j] == max) { r[result_num].a = i; r[result_num].b = j; result_num++; } } } } sort(r, r+result_num, cmp); out << max << endl; for (i = 0; i < result_num; i++) { out << s[r[i].a]; if (r[i].b != -1) out << " " << s[r[i].b]; out << endl; } } 
                
              
             
            
           
          
         
        
      
     
     
    
    
   
   
  
  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值