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 ...
随机推荐
- VMware 中安装虚拟机和宿主机通信
网络上对于三种连接模式说的很多了,这里就不在具体的说明了.此处采用的NAT模式连接虚拟机,让虚拟机和宿主机互相通讯,并且让虚拟机能访问互联网. 1.首先设置虚拟机的网络,如下图.通过如下操作进入虚拟机 ...
- Asp.Net Core实战(干货)
序言 使用.NET Core,团队可以更容易专注的在.net core上工作.比如核心类库(如System.Collections)的更改仍然需要与.NET Framework相同的活力,但是ASP. ...
- Jmeter(三十五)_分布式
jmeter分布式简单步骤说明: 1:添加远程服务器IP到配置文件 在JMETER_HOME / bin / jmeter.properties中,找到名为“ remote_hosts ” 的属性,并 ...
- H5 13-子元素选择器
13-子元素选择器 p{ color: red; } */ /* #identity>p{ color: blue; } */ div>ul>li>p{ color: purp ...
- NYOJ-16-矩形嵌套 记忆化搜索
#include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> ...
- An error occurred while updating the entries. See the inner exception for details.
EF插入或更新数据时出现错误提示:An error occurred while updating the entries. See the inner exception for details.的 ...
- 现有n 个乱序数,都大于 1000 ,让取排行榜前十,时间复杂度为o(n), top10, 或者 topK,应用场景榜单Top:10,堆实现Top k
一.topK python实现 def topk(k, lst): top = [0 for i in range(k)] #生成一个长度为K 的有序列表 for item in lst: #循环 ...
- Java基础之一反射
反射是框架设计的灵魂 (使用的前提条件:必须先得到代表的字节码的Class,Class类用于表示.class文件(字节码)) 一.反射的概述 JAVA反射机制是在运行状态中,对于任意一个类,都能够 ...
- CI框架在模型中切换读写库和读写库
如果你想在控制器中切换在application/config/database.php中配置好的数据库group,那么你可以参考这篇博客:CI框架在控制器中切换读写库和读写库 如果你是希望在模型中切换 ...
- 【Python3练习题 020】 求1+2!+3!+...+20!的和
方法一 import functools sum = 0 for i in range(1,21): sum = sum + functools.reduce(lambda x,y: x* ...