HDU 1247 Hat’s Words(字典树)题解
题意:给一个字符串集,要你给出n个字符串s,使s能被所给字符串集中的两个相加所得(ahat=a+hat)
思路:简单字典树题,注意查询的时候要判断所指next是否为NULL,否则会RE非法访问。
代价:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<cmath>
#include<string>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<iostream>
#include<algorithm>
#include<sstream>
#define ll long long
const int N=50005;
const int INF=1e9;
using namespace std;
char s[N][55];
struct Trie{
int num;
Trie *next[26];
Trie(){
num=0;
for(int i=0;i<26;i++){
next[i]=NULL;
}
}
};
Trie *root;
void insert(char s[]){
int len=strlen(s);
Trie *p=root;
for(int i=0;i<len;i++){
int v=s[i]-'a';
if(p->next[v]==NULL){
p->next[v]=new Trie();
}
p=p->next[v];
}
if(!p->num){
p->num=1;
}
}
int query(char s[],int rt){ //rt代表这是前半个单词还是后半个
int len=strlen(s),v;
Trie *p=root;
for(int i=0;i<len;i++){
v=s[i]-'a';
if(p->next[v]!=NULL){ //注意加这一步,防止RE
p=p->next[v];
}
else return 0;
if(rt==1 && p->num && i!=len-1){ //rt==1才能查看是否能由两个单词拼接
if(query(s+i+1,2)){
return -1;
}
}
}
return p->num;
}
/*void del(Trie *p){
if(p==NULL) return;
for(int i=0;i<26;i++){
if(p->next[i]!=NULL) del(p->next[i]);
}
delete p;
}*/
int main(){
int cnt=0;
int flag;
root=new Trie();
while(scanf("%s",s[cnt])!=EOF){
insert(s[cnt++]);
}
for(int i=0;i<cnt;i++){
flag=0;
flag=query(s[i],1);
if(flag==-1){
printf("%s\n",s[i]);
}
}
return 0;
}
HDU 1247 Hat’s Words(字典树)题解的更多相关文章
- HDU 1247 - Hat’s Words - [字典树水题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 Problem DescriptionA hat’s word is a word in the ...
- hdu 1247 Hat’s Words(字典树)
Hat's Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- hdoj 1247 Hat’s Words(字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 思路分析:题目要求找出在输入字符串中的满足要求(该字符串由输入的字符串中的两个字符串拼接而成)的 ...
- Hdu 1247 Hat's Words(Trie树)
Hat's Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- HDU 1247 Hat’s Words(字典树变形)
题目链接:pid=1247" target="_blank">http://acm.hdu.edu.cn/showproblem.php? pid=1247 Pro ...
- HDU 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 (字典树 && map)
分析:一開始是用递归做的,没做出来.于是就换了如今的数组.即,把每个输入的字符串都存入二维数组中,然后创建字典树.输入和创建完成后,開始查找. 事实上一開始就读错题目了,题目要求字符串是由其它两个输入 ...
- HDU 4757 Tree 可持久化字典树
Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4757 Des ...
随机推荐
- python2.7+pyqt4实现记事本基本功能
记事本程序: # coding:utf-8 import sys from PyQt4.QtGui import QMainWindow from PyQt4.QtGui import QApplic ...
- KVM VCPU线程调度问题的讨论
2017-11-15 今天闲着没有突然想了想VCPU线程调度的问题,具体描述如下: 当代表VCPU的线程获得控制权后,首先会通过KVM接口进入到内核,从内核进入到非根模式,那么此时站在全局调度器的点上 ...
- Shell初学(一)hello world
精简: 1.创建:可以使用 vi/vim 命令来创建文件如: test.sh ,扩展名并不影响脚本执行,写什么都可以. 2.hello_world: #!/bin/bash ...
- 003-spring cloud gateway-概述、Route模型、网关初始化配置过程、基本原理
一.概述 网关服务核心是将进入的请求正确合理的路由到下层具体的服务进行业务处理,由此可见网关服务的核心就是路由信息的构建. Spring Cloud Gateway旨在提供一种简单而有效的方式来路由到 ...
- [py]django url 参数/reverse和HttpResponseRedirect
参考 需要完成以下任务 - 访问http://127.0.0.1:8000/ 返回"hello maotai"或home.html - 访问http://127.0.0.1:800 ...
- LVS + Keepalived 实现高可用、负载均衡 Web 集群
简介: LVS 是 Linux Virtual Server 的简写,Linux 虚拟服务器的意思,是一个虚拟的服务器集群系统,此项目由章文嵩博士于 1998 年 5 月成立,是中国最早出现的自由软件 ...
- [LeetCode] 168. Excel Sheet Column Title_Easy tag: Math
Given a positive integer, return its corresponding column title as appear in an Excel sheet. For exa ...
- c++虚函数重写的权限问题
cbase.h: #ifndef CBASE_H #define CBASE_H #include<iostream> using std::cout; using std::endl; ...
- EF Code First学习笔记 初识Code First(转)
Code First是Entity Framework提供的一种新的编程模型.通过Code First我们可以在还没有建立数据库的情况下就开始编码,然后通过代码来生成数据库. 下面通过一个简单的示例来 ...
- git必备命令
git status 查看git的状态git add <path>的形式把我们<path>添加到索引库中,<path>可以是文件也可以是目录.git add -u ...