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. 51nod 1534 棋子游戏

    1534 棋子游戏 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 http://www.51nod.com/onlineJudg ...

  2. swiper隐藏再显示出现点击不了情况

    //初始化swiper var swiper = new Swiper('.swiper-container', { pagination: '.swiper-pagination', nextBut ...

  3. 2016-2017-2 《Java程序设计》第五周学习总结

    20155223 2006-2007-2 <Java程序设计>第五周学习总结 教材学习内容总结 第八章 try.catch语法:若程序发生错误,执行流程就调离错误点,然后比较catch括号 ...

  4. java学习第01天(搭建环境配置)

    搭建环境配置分为3个步骤 1.下载并安装 jdk使用1.8.0_151版本,下载地址(包含32位和64位):链接:https://pan.baidu.com/s/1qZau8oO 密码:0t5o 2. ...

  5. 去除zabbix calculate 模式下,有时候分母为零的情况(Cannot evaluate expression: division by zero. )

    zabbix的监控类型支持一种calculate的方式,可以对几个item结果进行简单的计算,但有时会出现分母为零的情况,这时候监控项就会报错 Cannot evaluate expression: ...

  6. python实现梯度下降法

    # coding:utf-8 import numpy as np import matplotlib.pyplot as plt x = np.arange(-5/2,5/2,0.01) y = - ...

  7. 函数嵌套>作用域>闭包函数

    一:函数对象 函数是第一类对象,即表示函数可以当做数据传递 可以被引用:把函数内存地址赋值给一个变量名,仍然遵循函数的调用规则. 可以被当做参数传递:传递的是函数的运行的结果#可以当做返回值 把函数作 ...

  8. Centos6.5下搭建nagios详解

    一.LAMP环境部署 1.安装php 1.安装yum源 rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-releas ...

  9. 【codeforces】【比赛题解】#920 Educational CF Round 37

    [A]浇花 题意: 一个线段上每个整点都有花,有的点有自动浇花的喷水器,有问几秒能浇完所有的花. 题解: 大模拟 #include<cstdio> #include<cstring& ...

  10. 聊天室(上篇)GatewayWorker 基础

    前言 本文的目的是基于 GatewayWorker 官方手册,梳理一次 GatewayWorker,并在实践中与 MVC 框架整合的思路(附最终的项目源码).如果你已经理解了整合这一块儿的知识,那么就 ...