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 ...
随机推荐
- LeetCode 1. twoSums
C++: vector<int> twoSum(vector<int>& nums, int target) { unordered_map<int, int&g ...
- 简析TCP的三次握手与四次挥手
TCP是什么? 具体的关于TCP是什么,我不打算详细的说了:当你看到这篇文章时,我想你也知道TCP的概念了,想要更深入的了解TCP的工作,我们就继续.它只是一个超级麻烦的协议,而它又是互联网的基础,也 ...
- Mac 10.7.*安装XCode3.2.6的方法
1.首先,在Xcode 3.2.6的磁盘映像(dmg文件)上点击右键,选择“磁盘工具”打开,如图1所示,转换成一个可读写的dmg文件,如图2所示. 图1 图2 转换好后双击它,让它在Finder里面显 ...
- python使用mysql的三个模块:mysql.connector、sqlalchemy、MySQLdb
在python中使用mysql其实很简单,只要先安装对应的模块即可,那么对应的模块都有什么?官方也没指定也没提供,pcat就推荐自己遇到的3个模块:mysql.connector.sqlalchemy ...
- 用Cython加速Python程序以及包装C程序简单测试
用Cython加速Python程序 我没有拼错,就是Cython,C+Python=Cython! 我们来看看Cython的威力,先运行下边的程序: import time def fib(n): i ...
- php错误 mysql_query():supplied argument is not a valid MySQL result resource
出现这种错误,原因是出现该错误的函数的参数出现了问题 参数出现问题有多种情况: 1.sql查询语句有问题,可能多了一个逗号,少了一个逗号,多了括号之类的: 2.与数据库连接的参数有问题,用户名.密码. ...
- Linux 下修改Tomcat使用的JVM内存大小
我的服务器的配置: # OS specific support. $var _must_ be set to either true or false. JAVA_OPTS="-Xms10 ...
- Oracle ODI系列之一(ODI知识模块)
Oracle ODI系列之一(ODI知识模块) ODI简介 ODI(Oracle Data Integrator)前身是Sunopsis Active Integration Platform ...
- 关于Android Launcher图标上面动态改变数字的实现
由于项目需要使用到类似小米应用商店的图标数字提示功能,谷歌百度了许多文章都没看到有真正意义上的实现(没有在国外网站上搜索),有实现在APP内部的一个ImageView上面更新数字的,当然这种太简单无非 ...
- 使用python抓取知乎日报的API数据
使用 urllib2 抓取数据时,最简单的方法是: import urllib2, json def getStartImage(): stream = urllib2.urlopen('http:/ ...