poj-2001(字典树)
题意:给你一堆字符串,我们定义一个字符串可以被缩写成一个字符串(必须是原字符串的前缀),问你每个字符串能辨识的前缀是什么,不能辨识意思是(ab,abc我们缩写成ab);
解题思路:可以用字典树解决,我们把刚开始的串存进去,然后在询问的时候,如果当前节点的字符只被一个串走过,那么肯定可以辨识;如果遍历完了找不到·,那么输出他自己;
代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<cstring>
#define maxn 50050
using namespace std;
int trie[maxn][30];
string s[2050];
string a;
int cnt[maxn];
int root;
int tot;
void build_trie(string s)
{
root=0;
int slen=s.size();
for(int i=0;i<slen;i++)
{
int id=s[i]-'a';
if(!trie[root][id])
{
trie[root][id]=++tot;
}
root=trie[root][id];
cnt[root]++;
}
}
int query(string s)
{
root=0;
int slen=s.size();
for(int i=0;i<slen;i++)
{
int id=s[i]-'a';
if(cnt[root]==1)
{
return i-1;
}
root=trie[root][id];
}
return slen-1;
}
int main()
{
int cot=0;
while(cin>>a)
{
s[++cot]=a;
build_trie(a);
}
for(int i=1;i<=cot;i++)
{
int pos=query(s[i]);
cout<<s[i]<<" ";
for(int j=0;j<=pos;j++)
cout<<s[i][j];
cout<<endl;
}
}
poj-2001(字典树)的更多相关文章
- POJ 2001 字典树(入门题)
#include<cstdio> #include<algorithm> #include<iostream> #include<cstring> #i ...
- POJ 2418 字典树
题目链接:http://poj.org/problem?id=2418 题意:给定一堆树的名字,现在问你每一棵树[无重复]的出现的百分比,并按树名的字典序输出 思路:最简单的就是用map来写,关于字典 ...
- POJ 2503 字典树
题目链接:http://poj.org/problem?id=2503 题意:给定一个词典,输入格式为[string1' 'string2] 意思是string2的值为string1. 然后给定一波 ...
- poj 3764 字典树
The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7332 Accepted: 1 ...
- POJ 2513 字典树+并查集+欧拉路径
Description: 给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 解题思路: 可以用图论中欧拉路的知识来解这道题,首先可以把木 ...
- POJ 2001 Shortest Prefixes(字典树)
题目地址:POJ 2001 考察的字典树,利用的是建树时将每个点仅仅要走过就累加.最后从根节点開始遍历,当遍历到仅仅有1次走过的时候,就说明这个地方是最短的独立前缀.然后记录下长度,输出就可以. 代码 ...
- poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12731 Accepted: 544 ...
- 字典树---2001 POJ Shortest Prefixes(找最短前缀)
做的第一道字典树的题,算比较水的: -->>>:传送门 代码: #include <stdio.h> #include<stdlib.h> #define M ...
- POJ 2001 Shortest Prefixes 【 trie树(别名字典树)】
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15574 Accepted: 671 ...
- poj 2503:Babelfish(字典树,经典题,字典翻译)
Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 30816 Accepted: 13283 Descr ...
随机推荐
- Android root检测方法小结
转载目的,之前主要应用这里的原理解决了,手机被某个APP检测为root过的手机的问题,记录后续可能参考. 出于安全原因,我们的应用程序不建议在已经root的设备上运行,所以需要检测是否设备已经root ...
- UVA - 10931-Parity
题意:1.输入一个数,将其转换为二进制.2.记录二进制中出现1的次数. 注意:转换二进制后直接输出,不能转换为十进制后输出 #include<iostream> #include<c ...
- Spark运行模式:cluster与client
When run SparkSubmit --class [mainClass], SparkSubmit will call a childMainClass which is 1. client ...
- 朱晔的互联网架构实践心得S1E7:三十种架构设计模式(上)
朱晔的互联网架构实践心得S1E7:三十种架构设计模式(上) [下载本文PDF进行阅读] 设计模式是前人通过大量的实践总结出来的一些经验总结和最佳实践.在经过多年的软件开发实践之后,回过头来去看23种设 ...
- Technical Development Guide---for Google
Technical Development Guide This guide provides tips and resources to help you develop your technica ...
- Python学习第二篇
list_num=list(range(1,1000001)) print(min(list_num)) print(max(list_num)) print(sum(list_num)) print ...
- anaconda安装win10
注意事项: 1.下载安装,双选勾勾 2.安装python3.5的版本 conda create -n tensorflow python=3.5 3.激活环境activate tensorflow 4 ...
- [2017BUAA软工助教]常见问题Q&A
软工常见问题Q&A 目录: 1. 转会相关 1.1 转会流程是什么样子的? 1.2 团队中多人要求转会怎么办?(如何解散团队) 1.3 为什么有人想要转会? 1.4 软件工程课为什么有这一环节 ...
- 转:Linux(Centos)之安装Nginx及注意事项
1.Nginx的简单说明 a. Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器,期初开发的目的就是为了代理电子邮件服务器室友:Igor Sysoev开发 ...
- 阿里云ECS服务器云监控(cloudmonitor)Go语言版本插件安装卸载与维护
云监控Go语言版本插件安装_主机监控_用户指南_云监控-阿里云https://help.aliyun.com/document_detail/97929.html 云监控cloudmonitor 1. ...