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

——————————————————————————题解

一道查字典的题

用了一堆以前没用过的东西或不熟练的东西……

总结一下

string substr(int pos = 0,int n = npos) const;//返回pos开始的n个字符组成的字符串,如果n很大就返回pos之后所有的字符

string &append(int n,char c);        //在当前字符串结尾添加n个字符c

string &erase(int pos = 0, int n = npos); //删除pos开始的n个字符,返回修改后的字符串

vector的unique操作

vector<pss >::iterator iter=unique(astr.begin(),astr.end()); astr.erase(iter,astr.end());

以及【捂脸】文件读入读出(来自usaco)

 FILE *fin  = fopen ("test.in", "r");
FILE *fout = fopen ("test.out", "w");
int a, b;
fscanf (fin, "%d %d", &a, &b);
fprintf (fout, "%d\n", a+b);
 /*
ID: ivorysi
LANG: C++
TASK: lgame
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <set>
#include <vector>
#include <string.h>
#define siji(i,x,y) for(int i=(x);i<=(y);++i)
#define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
#define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
#define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
#define inf 0x7fffffff
#define ivorysi
#define mo 97797977
#define hash 974711
#define base 47
#define pss pair<string,string>
#define MAXN 30005
#define fi first
#define se second
#define pii pair<int,int>
using namespace std;
struct node {
node *le[];
int end;
node() {
memset(le,,sizeof(le));
end=;
}
}*root;
int val[]= {,,,,,,,,,,,,,,,,,,,,,,,,,};
char word[],len;
int used[],ans;
vector< pss > astr;
void ins(char *s){
int l=strlen(s+);
node *p=root;
siji(i,,l) {
if(p->le[s[i]-'a']==) {
p->le[s[i]-'a']=new node;
}
p=p->le[s[i]-'a'];
}
p->end=;
}
void init() {
char str[];
scanf("%s",word+);
len=strlen(word+);
root=new node;
FILE *fin = fopen ("lgame.dict", "r");
while(fscanf(fin,"%s",str+) && str[]!='.') {
ins(str);
}
}
int srch(string str) {
if(str.length()==) return ;
node *p=root;
int gz=;
//____
//if(str=="ag") gz=1;
//_____
int flag=;
int res=;
//if(gz) printf("%d\n",str.length());
xiaosiji(i,,str.length()) {
//if(gz)printf("%d %d %d\n",i,flag,res);
/*if(gz) {
printf("-----------\n");
siji(i,0,25) {
printf("%d %c\n",p->le[i],i+'a');
}
}*/
if(p->le[str[i]-'a']==) {
flag=-;
break;
}
else {
p=p->le[str[i]-'a'];
res+=val[str[i]-'a']; }
}
if(p->end == ) flag=-;
return flag*res; }
void calc(string str,int l) {
xiaosiji(i,,l) {
int temp=srch(str.substr(,i+))+srch(str.substr(+i+,));
if(temp>ans) {
astr.clear();
astr.push_back(make_pair(str.substr(,i+),str.substr(+i+,)));
int k=astr.size();
if(astr[k-].fi>astr[k-].se && astr[k-].se!=""){
swap(astr[k-].fi,astr[k-].se);
}
ans=temp;
}
else if(temp==ans) {
astr.push_back(make_pair(str.substr(,i+),str.substr(+i+,)));
int k=astr.size();
if(astr[k-].fi>astr[k-].se && astr[k-].se!=""){
swap(astr[k-].fi,astr[k-].se);
}
}
}
}
void dfs(string str,int l) {
if(l>len) return;
siji(i,,len) {
if(!used[i]) {
str.append(,word[i]);
used[i]=;
calc(str,l+);
dfs(str,l+);
used[i]=;
str.erase(l,);
}
}
}
void solve() {
init();
dfs("",);
sort(astr.begin(),astr.end());
vector<pss >::iterator iter=unique(astr.begin(),astr.end());
astr.erase(iter,astr.end());
printf("%d\n",ans);
xiaosiji(i,,astr.size()) {
cout<<astr[i].fi;
if(astr[i].se!="") {
cout<<" "<<astr[i].se;
}
puts("");
}
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("lgame.in","r",stdin);
freopen("lgame.out","w",stdout);
#else
freopen("f1.in","r",stdin);
#endif
solve();
return ;
}

USACO 4.3 Letter Game (字典树)的更多相关文章

  1. LA 3942 - Remember the Word (字典树 + dp)

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  2. HDU 4287 Intelligent IME(字典树数组版)

    Intelligent IME Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. hdu4284之字典树

    Intelligent IME Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. BNU 27847——Cellphone Typing——————【字典树】

    Cellphone Typing Time Limit: 5000ms Memory Limit: 131072KB This problem will be judged on UVA. Origi ...

  5. UVALive 5029 字典树

    E - Encoded Barcodes Crawling in process...Crawling failedTime Limit:3000MS    Memory Limit:0KB    6 ...

  6. Trie(前缀树/字典树)及其应用

    Trie,又经常叫前缀树,字典树等等.它有很多变种,如后缀树,Radix Tree/Trie,PATRICIA tree,以及bitwise版本的crit-bit tree.当然很多名字的意义其实有交 ...

  7. ACM学习历程—HDU 4287 Intelligent IME(字典树 || map)

    Description We all use cell phone today. And we must be familiar with the intelligent English input ...

  8. hust 1605 - Gene recombination(bfs+字典树)

    1605 - Gene recombination Time Limit: 2s Memory Limit: 64MB Submissions: 264 Solved: 46 DESCRIPTION ...

  9. uva 11488 - Hyper Prefix Sets(字典树)

    H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of str ...

  10. 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design

    字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...

随机推荐

  1. day16 常用类(String、StringBuffer、StringBuilder)

    1.String是唯一一个可以直接用常量赋值的引用数据类型. String的常量也是一个对象. 数据段——字符串常量池. 2.每一个字符串常量对象在加载期放入字符串常量池. java对String常量 ...

  2. Vuex深入理解

    store下的index.js: import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) let store = new Vuex.St ...

  3. LazyMay:实现同步和异步任务的顺序执行

    在掘金看到的文章,流程控制同步和异步任务的顺序执行,收益匪浅,工作中能用到. 1.实现以下效果 实现一个LazyMan,可以按照以下方式调用: LazyMan(“Hank”)输出: Hi! This ...

  4. Linux基础命令【记录】

    后台运行详情:https://www.cnblogs.com/little-ant/p/3952424.html 查看端口.查找等命令 根据关键字查找文件信息: cat <文件名> | g ...

  5. tips 前端 bootstrap 嵌套行 嵌套列 溢出 宽度不正确 栅格化系统计算

    bootstrap 当嵌套列时 有时会出现很奇异的row 的width不对问题出现的情况时 <div class="row" > <!--row a--> ...

  6. Java并发编程原理与实战十四:Lock接口的认识和使用

    保证线程安全演进: synchronized volatile AtomicInteger Lock接口提供的方法: void lock():加锁 void unlock():解锁 void lock ...

  7. soj1022. Poor contestant Prob

    1022. Poor contestant Prob Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description As everyb ...

  8. ASP.NET根据IP获取省市地址

    1.在网站的跟路径下面添加 QQWry.dat 文件,这个文件是IP数据库文件 2.添加以下一个类 IPScanner     C# 代码   复制 public class IPScanner { ...

  9. Spring3.2 Contorller单元测试参数问题: java.lang.NoSuchMethodException

    使用3.2做单元测试的时候发现这个问题,因为之前都是用3.0中的配置适配器使用AnnotationMethodHandlerAdapter,到3.2中升级为RequestMappingHandlerA ...

  10. 【leetcode 简单】 第九十二题 第N个数字

    在无限的整数序列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...中找到第 n 个数字. 注意: n 是正数且在32为整形范围内 ( n < 231). 示例 1: ...