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. Java基础-位运算符Bitwise Operators

    Java基础-位运算符Bitwise Operators 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.位运算特点 位运算符分为按位与(&),按位或(|),按位异或(^ ...

  2. 安装SQL Sever数据库失败的解决办法

    视频链接:https://www.bilibili.com/video/av12651739/ 我安装了SQL Sever2014.遇到了好多好多问题啊,整的我都快疯了.大致遇到的问题和解决办法如下. ...

  3. 51 nod 1046 A^B Mod C

    1046 A^B Mod C 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 给出3个正整数A B C,求A^B Mod C.   例如,3 5 8,3^ ...

  4. Jenkins maven 构建乱码,修改file.encoding系统变量编码为UTF-8

    一切都是windows的控制台默认编码GBK问题 情景: 使用jenkins构建,console 输出的中文乱码.代码编码格式是utf-8,因为Jenkins会默认读取当前系统的编码格式,导致构建日志 ...

  5. vbs 解析 html 文档

    关于VBS采集,网上流行比较多的方法都是正则,其实 htmlfile 可以解析 html 代码,但如果 designMode 没开启的话,有时候会包安全提示信息.但是开启 designMode (@预 ...

  6. log4net记录系统错误日志到文本文件用法详解

    log4net是一个完全免费开源的插件,可以去官网下载源码. 一般系统操作日志不会用log4net,自己写代码存入数据库更方便合理,但是系统部署后运行在客户环境,难免会发生系统bug.崩溃.断网等无法 ...

  7. node.js、git、bootstrap等安装配置

    纯记录 一,安装node.js 1 官方网址 http://nodejs.org/  点击install 下载node-v0.10.22-x86.msi 2 安装,修改安装目录到d盘,一路next,无 ...

  8. 20155303 2016-2017-2 《Java程序设计》第八周学习总结

    20155303 2016-2017-2 <Java程序设计>第八周学习总结 目录 学习内容总结(Linux命令) 教材学习中的问题和解决过程 代码调试中的问题和解决过程 代码托管 上周考 ...

  9. openjudge-NOI 2.5-1789 算24

    题目链接:http://noi.openjudge.cn/ch0205/1789/ 题解: 并不是非常简单的搜索,需要考虑一些东西…… 首先有运算符优先级的限制,还有括号,数字的顺序也可以调整,如果只 ...

  10. shell用户管理->

    用户的添加与删除练习 -> 脚本1(if then) 思路:1.条件测试, 脚本使用案例, 创建用户[交互式创建] 1.怎么交互式 read -p 2.接收到对应字符串怎么创建用户 userad ...