USACO 4.3 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
——————————————————————————题解
一道查字典的题
用了一堆以前没用过的东西或不熟练的东西……
总结一下
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 (字典树)的更多相关文章
- LA 3942 - Remember the Word (字典树 + dp)
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- HDU 4287 Intelligent IME(字典树数组版)
Intelligent IME Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu4284之字典树
Intelligent IME Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- BNU 27847——Cellphone Typing——————【字典树】
Cellphone Typing Time Limit: 5000ms Memory Limit: 131072KB This problem will be judged on UVA. Origi ...
- UVALive 5029 字典树
E - Encoded Barcodes Crawling in process...Crawling failedTime Limit:3000MS Memory Limit:0KB 6 ...
- Trie(前缀树/字典树)及其应用
Trie,又经常叫前缀树,字典树等等.它有很多变种,如后缀树,Radix Tree/Trie,PATRICIA tree,以及bitwise版本的crit-bit tree.当然很多名字的意义其实有交 ...
- ACM学习历程—HDU 4287 Intelligent IME(字典树 || map)
Description We all use cell phone today. And we must be familiar with the intelligent English input ...
- hust 1605 - Gene recombination(bfs+字典树)
1605 - Gene recombination Time Limit: 2s Memory Limit: 64MB Submissions: 264 Solved: 46 DESCRIPTION ...
- uva 11488 - Hyper Prefix Sets(字典树)
H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of str ...
- 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design
字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...
随机推荐
- RGB色彩对照表
RGB色彩对照表 #FFFFFF #FFFFF0 #FFFFE0 #FFFF00 #FFFAFA #FFFAF0 #FFFACD #FFF8DC #FFF68F ...
- 外网IP和内网IP的区别
这两天遇到一个bug,折腾的够呛,已经上线的项目,出现了个人登录不上的情况,瞬间整个人都不好了,首先找问题,在本地和测试服务器上都没问题,打包发布到正式环境就出现问题了,刚开始我看不了日志,日志要找别 ...
- bzoj千题计划162:bzoj2006: [NOI2010]超级钢琴
http://www.lydsy.com/JudgeOnline/problem.php?id=2006 输出最大的k个 sum[r]-sum[l-1] (L<=r-l+1<=R) 之和 ...
- codevs 1500 后缀排序
codevs 1500 后缀排序 http://codevs.cn/problem/1500/ 时间限制: 1 s 空间限制: 128000 KB 题目描述 Description 天凯是MI ...
- Matlab debug
输入彩色,imwrite保存黑白图片,imwrite的维度错误. 程序如下,正常图像,少了一个维度imwrite,把图片展开,是一个二维的灰色图像(R=G=B),.如果限定了第二维,也是一个灰色图像. ...
- JavaScript实现单向链表
JavaScript 本身提供了十分好用的数据类型,以满足大家的日常使用.单靠 Array 和 Object 也的确足够应付日常的绝大部分需求,这也导致了很多前端er对数据结构这一块不是十分的了解. ...
- <转>Android APP字体大小,不随系统的字体大小变化而变化的方法
从android4.0起系统设置的”显示“提供设置字体大小的选项.这个设置直接会影响到所有sp为单位的字体适配,所以很多app在设置了系统字体后瞬间变得面目全非.下面是解决方案 Resources r ...
- Cloudera Manager Admin控制台启动不起来
这几天都在搞大数据这一块,由于以前自己在弄hadoop等安装的时候特别的费劲,于是乎找到了广大程序员的福音——cloudera manager,但是第一步安装好了以后无法启动,再三思考+百度发现: 通 ...
- Dream------Java--ant zip 对压缩文件进行指定位置的修改
ant zip 对压缩文件进行指定位置的修改 实现功能: 对2中文件进行修改: 需求: 在XX文件中,从二进制流的200字节位置开始,往后的30位字节数量.插入一个值 由于涉及到公司内部,不方便写太多 ...
- 查看oracle数据库日志存放位置
1,默认情况下,oracle的日志文件记录在$ORACLE/rdbms/log目录下 [oracle@oracle log]$ pwd /home/oracle/oracle/product/11.2 ...