题目链接:https://vjudge.net/problem/HDU-1247

题意:给定n个字符串(n<=50000),判断其中哪些字符串恰能由另外两个不同的字符串连接而成。

思路:

  暴力字典树即可。右n个字符串建树,然后把每个字符串拆分判断两部分是否都在树中。

AC code:

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std; const int maxn=;
const int maxm=1e6+;
int n,trie[maxm][],key[maxm],cnt;
char str[maxn][]; void insert(char *s){
int len=strlen(s),u=;
for(int i=;i<len;++i){
int t=s[i]-'a';
if(!trie[u][t]){
++cnt;
memset(trie[cnt],,sizeof(trie[cnt]));
key[cnt]=;
trie[u][t]=cnt;
}
u=trie[u][t];
if(i==len-) key[u]=;
}
} bool query(int k,int l,int r){
int u=;
for(int i=l;i<=r;++i){
int t=str[k][i]-'a';
if(!trie[u][t]) return false;
u=trie[u][t];
}
if(!key[u]) return false;
else return true;
} int main(){
memset(trie[],,sizeof(trie[]));
while(~scanf("%s",str[++n])){
insert(str[n]);
}
--n;
for(int i=;i<=n;++i){
int flag=,len=strlen(str[i]);
for(int j=;j<=len-;++j)
if(query(i,,j-)&&query(i,j,len-)){
flag=;
break;
}
if(flag) printf("%s\n",str[i]);
}
return ;
}

hdoj1247(字典树)的更多相关文章

  1. 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)

    前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...

  2. [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  3. 字典树+博弈 CF 455B A Lot of Games(接龙游戏)

    题目链接 题意: A和B轮流在建造一个字,每次添加一个字符,要求是给定的n个串的某一个的前缀,不能添加字符的人输掉游戏,输掉的人先手下一轮的游戏.问A先手,经过k轮游戏,最后胜利的人是谁. 思路: 很 ...

  4. 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)

    萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...

  5. 山东第一届省赛1001 Phone Number(字典树)

    Phone Number Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 We know that if a phone numb ...

  6. 字典树 - A Poet Computer

    The ACM team is working on an AI project called (Eih Eye Three) that allows computers to write poems ...

  7. trie字典树详解及应用

    原文链接    http://www.cnblogs.com/freewater/archive/2012/09/11/2680480.html Trie树详解及其应用   一.知识简介        ...

  8. HDU1671 字典树

    Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. *HDU1251 字典树

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

随机推荐

  1. Sklearn多元线性回归

    Sklearn多元线性回归 1 正文 2 参考资料 Sklearn多元线性回归

  2. shell history 命令

    1.history命令可以显示历史执行过的命令: 2.使用!+序号执行该序号对应的命令: 例子 $ history sed 's/haha/hello/g' test cat test cat tes ...

  3. Debian9安装QT5.12.3

    打开虚拟机,打开火狐浏览器,输入网址下载QT5.12(linux版本,约13.G) download.qt.io/archive/qt/5.12/5.12.0/ 文件默认下载在Downloads文件夹 ...

  4. [Web前端] mac chrome 浏览器强制刷新,清除浏览器缓存

    本文链接:https://blog.csdn.net/zeroyulong/article/details/79806156 mac 强制刷新:command+shift+r mac 普通刷新:com ...

  5. 阿里物联网平台(一)Windows系统+VS2017 模拟设备端接入

    https://blog.csdn.net/panwen1111/article/details/88365636 一.阿里物联网平台 平台地址:https://account.aliyun.com ...

  6. gdbhooks 栈信息

    https://devguide.python.org/gdb/ https://sourceware.org/gdb/current/onlinedocs/gdb/Python.html#Pytho ...

  7. ArcMap与快捷键冲突

    ArcMap与快捷键冲突 商务合作,科技咨询,版权转让:向日葵,135-4855__4328,xiexiaokui#qq.com 问题:armap进入鼠标自动导航状态,arcmap失控,系统紊乱,导致 ...

  8. Windows安装Redis并添加本地自启动服务并解决客户端dll报错

    参考文章:https://blog.csdn.net/realjh/article/details/82026160 Redis下载: https://github.com/MicrosoftArch ...

  9. pip 安装,更新模块

    moudle_name:是对应的模块名:请自行更换为自己需要更新的模块名 查看所有可更新的模块: pip list --outdated 更新某一个模块: pip install --upgrade ...

  10. html表格导出Excel的实例

    1. 拼成出完整的HMTL的Table代码片段后,转成二进制byte[]类型并存入数据库中,供下载时调出来使用. System.Text.StringBuilder sb = new StringBu ...