hdoj1247(字典树)
2024-08-24 23:41:03
题目链接: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 ;
}
最新文章
- Lucene4.1 视频学习
- 一篇让Java程序猿随时可以翻看的Oracle总结
- 基于淘宝开源Tair分布式KV存储引擎的整合部署
- Inside TSQL Querying - Chapter 1. Logical Query Processing
- LinkedHashMap介绍
- saltstack实战4--综合练习1
- ▲▲▲▲▲▲▲▲▲▲▲yum源的配置(本地和ftp)▲▲▲▲▲▲▲▲▲▲▲▲▲v
- GCC(警告.优化以及调试选项)
- JavaScript路线
- Visual Studio 2017的安装与使用
- 面试题07_用两个栈实现队列——剑指offer系列
- EXCEPTION:FATAL: UNABLE TO CREATE ‘…GIT/INDEX.LOCK’ FILE EXISTS
- K好数
- eclipse git 报 git: 401 Unauthorized
- Angular material mat-icon 资源参考_Editor
- Vue-cli 工具 / 通过 Vue-cli 工具重构 todoList
- 详解Python中的生成器表达式(generator expression)
- C语言进阶——变量属性05
- php中各种操作字符串和时间戳的代码关键词
- 关于Banner设计的促销氛围