Hat’s Words(字典树)
Hat’s Words
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11314 Accepted Submission(s): 4041
You are to find all the hat’s words in a dictionary.
input consists of a number of lowercase words, one per line, in
alphabetical order. There will be no more than 50,000 words.
Only one case.
ahat
hat
hatword
hziee
word
hatword
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const int MAXN=;
int ch[MAXN][],word[MAXN],val[MAXN];
char dt[][];
int sz;
void initial(){
sz=;
mem(ch[],);mem(word,);mem(val,);
}
void join(char *s){
int len=strlen(s),k=,j;
for(int i=;i<len;i++){
j=s[i]-'a';
if(!ch[k][j]){
mem(ch[sz],);
// val[sz]=0;
ch[k][j]=sz++;
}
k=ch[k][j];
word[k]++;
}
val[k]=;
}
bool find(char *s){
int len=strlen(s),k=,j;
for(int i=;i<len;i++){
j=s[i]-'a';
k=ch[k][j];
if(!word[k])return false;
}
if(val[k])return true;
else return false;
}
int main(){
initial();
int t=;
char s[],l[],r[];
while(~scanf("%s",dt[t]))join(dt[t++]);
for(int i=;i<t;i++){
int len=strlen(dt[i]);
if(len<=)continue;
for(int j=;j<len;j++){
strcpy(r,dt[i]+j);
strcpy(l,dt[i]);
l[j]='\0';
if(find(l)&&find(r)){
puts(dt[i]);break;
}
}
}
return ;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define P_ printf(" ")
const int INF=0x3f3f3f3f;
const int MAXN=1000010;//要开的足够大
int ch[MAXN][30];
int word[MAXN];
char str[50010][110];
int val[MAXN];
int sz;
int N;
char l[110],r[110];
void insert(char *s){
int k=0,j;
for(int i=0;s[i];i++){
j=s[i]-'a';
if(!ch[k][j]){
mem(ch[sz],0);
ch[k][j]=sz++;
}
k=ch[k][j];
word[k]++;
}
val[k]=1;
}
bool find(char *s){
int k=0,j;
for(int i=0;s[i];i++){
j=s[i]-'a';
k=ch[k][j];
if(!word[k])return false;
}
if(val[k]!=1)return false;
return true;
} int main(){
int tp=0;
sz=1;
mem(ch[0],0);mem(val,0);mem(word,0);
while(~scanf("%s",str[tp]))insert(str[tp++]);
// printf("%d\n",tp);
for(int i=0;i<tp;i++){
for(int j=0;str[i][j];j++){
strcpy(l,str[i]);
l[j]='\0';
strcpy(r,str[i]+j);
// printf("**%s %s\n",l,r);
if(find(l)&&find(r)){
puts(str[i]);break;
}
}
}
return 0;
}
Hat’s Words(字典树)的更多相关文章
- hdu 1247 Hat’s Words(字典树)
Hat's Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- HDU 1247 - Hat’s Words - [字典树水题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 Problem DescriptionA hat’s word is a word in the ...
- Hat’s Words(字典树)
Problem Description A hat's word is a word in the dictionary that is the concatenation of exactly tw ...
- hdoj 1247 Hat’s Words(字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 思路分析:题目要求找出在输入字符串中的满足要求(该字符串由输入的字符串中的两个字符串拼接而成)的 ...
- hdu 1247:Hat’s Words(字典树,经典题)
Hat’s Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- HDU 1247 Hat’s Words(字典树)题解
题意:给一个字符串集,要你给出n个字符串s,使s能被所给字符串集中的两个相加所得(ahat=a+hat) 思路:简单字典树题,注意查询的时候要判断所指next是否为NULL,否则会RE非法访问. 代价 ...
- HDU 1247 Hat’s Words (字典树 && map)
分析:一開始是用递归做的,没做出来.于是就换了如今的数组.即,把每个输入的字符串都存入二维数组中,然后创建字典树.输入和创建完成后,開始查找. 事实上一開始就读错题目了,题目要求字符串是由其它两个输入 ...
- hdu1 247 Hat’s Words(字典树)
Hat’s Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- Hat’s Words(字典树的运用)
个人心得:通过这道题,对于树的运用又加深了一点,字典树有着他独特的特点,那个指针的一直转换着实让我好生想半天, 不得不佩服这些发明算法人的大脑. 这题的解决方法还是从网上找到的,还好算法是自己实现得, ...
- HDU 1247 Hat’s Words(字典树变形)
题目链接:pid=1247" target="_blank">http://acm.hdu.edu.cn/showproblem.php? pid=1247 Pro ...
随机推荐
- BZOJ 4321: queue2( dp )
dp(i, j, 1)表示前i个, 有j对是不合法的, i和i-1是在一起的. dp(i, j, 0)表示前i个, 有j对是不合法的, i和i-1不在一起的. 转移我们只需考虑是多了一对不合法的还是少 ...
- aJax学习之Ajax工作原理
转自:http://www.cnblogs.com/mingmingruyuedlut/archive/2011/10/18/2216553.html 在写这篇文章之前,曾经写过一篇关于AJAX技术的 ...
- MyEclipse下Struts2配置使用和Ajax、JSON的配合
原创文章,转载请注明:MyEclipse下Struts2配置使用和Ajax.JSON的配合 By Lucio.Yang 新手,初学Struts2的配置,同时尝试与Ajax通过JSON交互.首先介绍M ...
- 类 BufferedReader
以前学习的时候也没有太在意,在项目中使用到了才发现呵呵 1.读取一个txt文件,方法很多种我使用了字符流来读取(为了方便) FileReader fr = new FileReader("f ...
- 如何取消一个本地svn目录与svn的联系(即恢复原有图标等)
在使用svn 的时候容易手抖错选update地址,使其目录所有同级文件夹上出现蓝色“?”图样,非常烦人,下面记录一下解决方案. 首先在该目录下打开同级文件件,工具→文件夹选项→查看→隐藏文件和文件夹→ ...
- Github Blog 搭建手册
http://www.ilehao.com/blog/2012/11/11/github-blog-config/ http://www.freebuf.com/articles/web/25613. ...
- 修改Eclipse的WorkSpace保持数[转载]
最近用Eclipse开发特别多,我个人习惯每一个项目一个WorkSpace,这样的话代码干净.而且当项目之前编码规范不一样时,也不会彼此影响.但项目一多,Eclipse默认只保存5个WorkSpace ...
- POJ 3450 Corporate Identity(KMP)
[题目链接] http://poj.org/problem?id=3450 [题目大意] 求k个字符串的最长公共子串,如果有多个答案,则输出字典序最小的. [题解] 我们对第一个串的每一个后缀和其余所 ...
- 四核网络机顶盒芯片局势分析(开放市场):rk3128将会成为四核主流
开放市场:不包含小米.乐视等大品牌闭环生态系统的市场. 今年四核网络播放器以全志的a31s独领风骚.英菲克以绝对优势率先各大白牌品牌公司.只是随着时间的推移,全志的a31s不适应市场主流.因为芯片没有 ...
- nginx 使用安装问题及解决方案
如何安装清参考:http://www.runoob.com/linux/nginx-install-setup.html 可以先不用配置,等理解后在配置. 在启动nginx时,出现提示: nginx: ...