HDU 1247 Hat's Words (字典树)
来源:互联网 发布:行楷字帖 知乎 编辑:程序博客网 时间:2024/05/17 09:08
【题目链接】click here~~
【题目大意】A hat’s word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary. ,找出由两个子字符串组成的字符串。
【解题思路】字典树
#include <bits/stdc++.h>using namespace std;const int N=5*1e4+100;const int MOD=998244353;#pragma comment(linker,"/STACK:102400000,102400000")int n,m,k,tot;char word[N][27];typedef long long LL;struct dictree{ int count; bool ok; dictree *tree[27]; dictree() { ok=false; memset(tree,0,sizeof(tree)); }};void insert(dictree *head,char str[]){ dictree *p=head; int i=0; while(str[i]) { int k=(int)(str[i++]-'a'); if(p->tree[k]==NULL) p->tree[k]=new dictree(); p=p->tree[k]; } p->ok=true;}bool search(dictree *head,char str[]){ int i=0,top=0,num[N]; dictree *p=head; while(str[i]) { int k=(int)str[i++]-'a'; if(p->tree[k]==NULL) return false; p=p->tree[k]; if(p->ok&&str[i]) num[top++]=i; } while(top) { bool okk=true; i=num[--top]; p=head; while(str[i]) { int k=(int)str[i++]-'a'; if(p->tree[k]==NULL) { okk=false; break; } p=p->tree[k]; } if(okk&&p->ok) return true; } return false;}int main(){ int len=0; dictree *head=new dictree(); while(gets(word[len])) { insert(head,word[len]); len++; } for(int i=0; i<len; ++i) { if(search(head,word[i])) printf("%s\n",word[i]); } return 0;}</span>
set容器:
#include <bits/stdc++.h>using namespace std;int main(){ string str; set<string>sa,sb; while(cin>>str) sa.insert(str); set<string>::iterator be=sa.begin(),en=sa.end(),op,opp; for(; be!=en; be++) { int Count=be->size(); for(op=be,op++; op!=en; op++) { if(be->compare(op->substr(0,Count))==0) { if(sa.count(op->substr(Count,op->size()-Count))) sb.insert(*op); } else break; } } for(opp=sb.begin(); opp!=sb.end(); opp++) cout<<*opp<<endl; return 0;}</span>
0 0
- HDU 1247 Hat’s Words(字典树)
- hdu 1247 字典树 Hat’s Words
- HDU 1247 Hat’s Words 字典树
- hdu 1247 Hat’s Words 字典树
- HDU 1247 Hat’s Words(字典树)
- hdu 1247 Hat’s Words(字典树)
- hdu 1247 Hat’s Words字典树
- hdu 1247 Hat’s Words(字典树)
- hdu 1247 Hat’s Words 字典树
- 【字典树】HDU 1247 Hat’s Words
- HDU--1247:Hat’s Words (字典树)
- hdu 1247 Hat’s Words(字典树)
- [字典树] HDU 1247 - Hat’s Words
- hdu 1247 Hat’s Words (字典树)
- HDU 1247 Hat’s Words 字典树
- HDU - 1247 Hat’s Words(字典树)
- hdu 1247 Hat’s Words 字典树
- hdu 1247 Hat’s Words(字典树)
- Leetcode37: Implement strStr()
- eclipse+php+wamp配置
- Python学习之库引用错误
- java程序员如何学习SAP Hybris
- 工作线程的全局函数中如何控制picture control进行图像显示
- HDU 1247 Hat's Words (字典树)
- android与SharedPreference
- 写了个算分压电阻阻值的MATLAB小程序
- 在Mac电脑上为Dash制作docSet文档
- 2015年研三学习计划
- Struts2(三)访问和添加request、session、application属性
- 到底是什么样?——不用争论。
- Java文件打包成exe,在未配置安装JDK和未配置的电脑上运行
- 计算中国农历(一)