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. json转java对象

    用了平台之后很少再接触到java和js的底层代码,前几天远程帮一个萌新远程调试代码,这个萌新按照网上的教程去将json字符转java对象却一直报错.真相是它的json字符串格式不对,他的明明是一个数组 ...

  2. 《剑指offer》 面试题43 n个骰子的点数 (java)

    引言:写这篇文章的初衷只是想做个笔记,因为这道题代码量有点大,有点抽象,而书上并没有详细的注释.为了加深印象和便于下次复习,做个记录. 原题:把n个骰子扔到地上,所有骰子朝上一面的点数之后为s. 输入 ...

  3. 科学计算三维可视化---Mlab基础(改变物体的外观颜色)

    import numpy as np from mayavi import mlab #建立数据 x,y = np.mgrid[-::200j,-::200j] z = *np.sin(x*y)/(x ...

  4. Dubbo学习笔记2:Dubbo服务提供端与消费端应用的搭建

    Demo结构介绍 Demo使用Maven聚合功能,里面有三个模块,目录如下: 其中Consumer模块为服务消费者,里面TestConsumer和consumer.xml组成了基于Spring配置方式 ...

  5. bzoj千题计划125:bzoj1037: [ZJOI2008]生日聚会Party

    http://www.lydsy.com/JudgeOnline/problem.php?id=1037 一个区间是否满足 任意连续的一段,男孩与女孩的数目之差不超过k, 取决于男孩与女孩数目之差的最 ...

  6. Java获取精确到秒的时间戳

    1.时间戳简介: 时间戳的定义:通常是一个字符序列,唯一地标识某一刻的时间.数字时间戳技术是数字签名技术一种变种的应用.是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01 ...

  7. 有用的JavaScript开发小建议

    这篇文章将向你分享一些不为人知的但很有用的JavaScript小建议,对那些刚涉及使用JavaScript编程语言的初级开发者应该有很大的帮助. 1. 用数组长度截取数组 我们都知道,对象都是通过使用 ...

  8. 【leetcode 简单】 第八十八题 猜数字大小

    我们正在玩一个猜数字游戏. 游戏规则如下: 我从 1 到 n 选择一个数字. 你需要猜我选择了哪个数字. 每次你猜错了,我会告诉你这个数字是大了还是小了. 你调用一个预先定义好的接口 guess(in ...

  9. python 操作 memcache

    目录 Memcached Memcached安装 python操作Memcached Memcache模块常用方法 Memcached Memcached是一个高性能的分布式内存对象缓存系统,用于动态 ...

  10. 关于在函数中使用Array.prototype.slice.call而不是直接用slice

    arguments是每个函数在运行的时候自动获得的一个近似数组的对象(除了length外没有其他属性),这个arguments对象其实并不是Array,所以没有slice方法. Array.proto ...