poj2011
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 17608 | Accepted: 7658 |
Description
In the sample input below, "carbohydrate" can be abbreviated to "carboh", but it cannot be abbreviated to "carbo" (or anything shorter) because there are other words in the list that begin with "carbo".
An exact match will override a prefix match. For example, the prefix "car" matches the given word "car" exactly. Therefore, it is understood without ambiguity that "car" is an abbreviation for "car" , not for "carriage" or any of the other words in the list that begins with "car".
Input
Output
Sample Input
carbohydrate
cart
carburetor
caramel
caribou
carbonic
cartilage
carbon
carriage
carton
car
carbonate
Sample Output
carbohydrate carboh
cart cart
carburetor carbu
caramel cara
caribou cari
carbonic carboni
cartilage carti
carbon carbon
carriage carr
carton carto
car car
carbonate carbona
Source
题意:
找出能唯一标示一个字符串的最短前缀,如果找不出,就输出该字符串
题解:
在裸字典树的基础上,trie[now].w表示now这个节点被用过几次,当我们扫一个单词时,
如果它的某一个字母只用过一次,即只有它用过,那么这以前就是它的前缀。
AC代码:
#include<cstdio>
#include<cstring>
using namespace std;
const int N=;
const int M=;
struct node{
int next[M];
int w;
}tire[N*M];
char s[N][M];
int cnt,n;
void build(int num){
int now=,len=strlen(s[num]);
for(int i=;i<len;i++){
int x=s[num][i]-'a'+;
if(!tire[now].next[x])
tire[now].next[x]=++cnt;
now=tire[now].next[x];
tire[now].w++;
}
}
void query(int num){
int now=,len=strlen(s[num]);
for(int i=;i<len;i++){
int x=s[num][i]-'a'+;
now=tire[now].next[x];
printf("%c",s[num][i]);
if(tire[now].w==) break;
}
}
int main(){
while(scanf("%s",s[++n])==) build(n);n--;
for(int i=;i<=n;i++) printf("%s ",s[i]),query(i),putchar('\n');
return ;
}
poj2011的更多相关文章
随机推荐
- CAD执行一个带参数的命令(com接口VB语言)
主要用到函数说明: MxDrawXCustomFunction::Mx_SendStringToExecute 执行一个带参数的命令.详细说明如下: 参数 说明 CString sCmaName 命令 ...
- 牛客多校Round 5
Solved:3 rank:195 F. take 官方题解:小 A 在打开第 i 个箱子后会交换手中的钻石和第 i 个箱子中的钻石 当且仅当第 i个箱子的钻石是前 i 个箱子打开后出现的所有钻石里最 ...
- LeetCode_16 3SumCloest
3Sum Closest Given an array nums of n integers and an integer target, find three integers in nums su ...
- MySQL6
MySQL数据库6 1. 集群概述 性能达到瓶颈的解决方案 Scale Up 向上扩展能力,如增加更好的硬件固态磁盘,加大内存等,成本高,效果不显著 Scale Out 向外扩展,例如建立更多的服务器 ...
- python爬取豆瓣小组700+话题加回复啦啦啦python open file with a variable name
需求:爬取豆瓣小组所有话题(话题title,内容,作者,发布时间),及回复(最佳回复,普通回复,回复_回复,翻页回复,0回复) 解决:1. 先爬取小组下,所有的主题链接,通过定位nextpage翻页获 ...
- day21 01 包的初识
day21 01包的初识 包:把解决一类问题的模块放在同一个文件夹里面-----包(一个包里面通常会含有_init_.py文件(python2里面必须有),但是后面的就没有要求一定要有了) 同样导入的 ...
- importdata-- matlab
source file: test.dat *************************** Day1 Day2 Day3 Day4 Day5 Day6 Day795.01 76.2 ...
- opengl 对投影变化函数的理解
投影变化分两种: 1 . 平行投影 2 . 透视投影 投影变化的设置一般放在reshape函数当中调用 每次要对投影变化进行操作的时候我们需要修改矩阵的变化模式,指定它为投影变化 glMa ...
- [K/3Cloud]屏蔽页签的关闭按钮
如图,屏蔽企业门户的X关闭按钮. this.View.GetControl<TabControl>("FMainTab").SetItemCloseable(1, fa ...
- [bzoj1577][Usaco2009 Feb]庙会捷运Fair Shuttle_贪心_线段树
庙会捷运 Fair Shuttle bzoj-1577 Usaco-2009 Feb 题目大意:有一辆公交车从1走到n.有m群奶牛从$S_i$到$E_i$,第i群奶牛有$W_i$只.车有一个容量c.问 ...