题意:给你一堆字符串,我们定义一个字符串可以被缩写成一个字符串(必须是原字符串的前缀),问你每个字符串能辨识的前缀是什么,不能辨识意思是(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(字典树)的更多相关文章

  1. POJ 2001 字典树(入门题)

    #include<cstdio> #include<algorithm> #include<iostream> #include<cstring> #i ...

  2. POJ 2418 字典树

    题目链接:http://poj.org/problem?id=2418 题意:给定一堆树的名字,现在问你每一棵树[无重复]的出现的百分比,并按树名的字典序输出 思路:最简单的就是用map来写,关于字典 ...

  3. POJ 2503 字典树

    题目链接:http://poj.org/problem?id=2503 题意:给定一个词典,输入格式为[string1' 'string2]  意思是string2的值为string1. 然后给定一波 ...

  4. poj 3764 字典树

    The xor-longest Path Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7332   Accepted: 1 ...

  5. POJ 2513 字典树+并查集+欧拉路径

    Description: 给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 解题思路: 可以用图论中欧拉路的知识来解这道题,首先可以把木 ...

  6. POJ 2001 Shortest Prefixes(字典树)

    题目地址:POJ 2001 考察的字典树,利用的是建树时将每个点仅仅要走过就累加.最后从根节点開始遍历,当遍历到仅仅有1次走过的时候,就说明这个地方是最短的独立前缀.然后记录下长度,输出就可以. 代码 ...

  7. poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12731   Accepted: 544 ...

  8. 字典树---2001 POJ Shortest Prefixes(找最短前缀)

    做的第一道字典树的题,算比较水的: -->>>:传送门 代码: #include <stdio.h> #include<stdlib.h> #define M ...

  9. POJ 2001 Shortest Prefixes 【 trie树(别名字典树)】

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15574   Accepted: 671 ...

  10. poj 2503:Babelfish(字典树,经典题,字典翻译)

    Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 30816   Accepted: 13283 Descr ...

随机推荐

  1. python:利用logbook模块管理日志

    日志管理作为软件项目的通用部分,无论是开发还是自动化测试过程中,都显得尤为重要. 最初是打算利用python的logging模块来管理日志的,后来看了些github及其他人的自动化框架设计,做了个比对 ...

  2. 移动端h5页面编写样式重置

    @charset "UTF-8"; /* stylelint-enable */ /* 重置样式 */ * { -webkit-tap-highlight-color: trans ...

  3. Jmeter自定义Java请求,继承AbstractJavaSamplerClient

    首先,使用Eclipse新建一个项目,然后从Jmeter的lib/ext目录下中拷贝ApacheJMeter_java.jar和ApacheJMeter_core.jar两个文件,然后引入这两个JAR ...

  4. C#发邮件_EmailHelper

    EmailHelper类 public class EmailHelper { /// <summary> /// 发送邮件 /// </summary> /// <pa ...

  5. 基于webpack+react+antd 项目构建

    工欲善其事必先利其器,学习React也是如此. 下面分享一篇基于webpack+react+antd 项目构建的好文章, https://blog.hduzplus.xyz/articles/2017 ...

  6. Groovy语言学习--语法基础(2)

    集合和闭包 因为之前没接触过C++等,对指针也一窍不通.个人不成熟的了解 闭包是一种数据类型,可以很方便的执行一段独立的代码 简化方法的调用 package groovy /** * Groovy容器 ...

  7. Quartz.NET 任务调度教程。

    https://www.cnblogs.com/yaopengfei/p/9216229.html

  8. 从 0 到 1 实现 React 系列 —— 2.组件和 state|props

    看源码一个痛处是会陷进理不顺主干的困局中,本系列文章在实现一个 (x)react 的同时理顺 React 框架的主干内容(JSX/虚拟DOM/组件/生命周期/diff算法/setState/ref/. ...

  9. linux screen 工具

    一.背景 系统管理员经常需要SSH 或者telent 远程登录到Linux 服务器,经常运行一些需要很长时间才能完成的任务,比如系统备份.ftp 传输等等.通常情况下我们都是为每一个这样的任务开一个远 ...

  10. VS2012添加数据库连接时报错,未能加载文件或程序集microsoft.sqlserver.management.sdk.sfc

    今天在VS2012中添加数据库连接时报错 未能加载文件或程序集microsoft.sqlserver.management.sdk.sfc,Version=11.0 查了很多资料,最后下载安装了Sha ...