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

Problem Description
A hat’s word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary.
You are to find all the hat’s words in a dictionary.
 
Input
Standard
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.
 
Output
Your output should contain all the hat’s words, one per line, in alphabetical order.
 
Sample Input
a
ahat
hat
hatword
hziee
word
 
Sample Output
ahat
hatword
 
题解:判断单词是否是由两个单词合并得到,加了个val判断是否为一个完整的单词;本来还以为是只与hat结合呐,神经的只减去hat,谁知道,自己想错了。。。是两个单词的结合;
代码:
 #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;
}

  

最新文章

  1. Window系统性能获取帮助类
  2. [ActionScript 3.0] 分页排版
  3. 创建一个没有边框的并添加自定义文字的UISegmentedControl
  4. 把浏览器的私有模式添加到VS中
  5. ReactNative 踩坑小计
  6. MySql数据库3【优化2】sql语句的优化
  7. C与C++
  8. VMWare11虚拟机安装OSX10.9系统资源下载及问题解决
  9. QT中LineEdit、TextEdit 、PlainTextEdit 三个控件的区别
  10. Unsupervised Learning and Text Mining of Emotion Terms Using R
  11. 【转】花开正当时,十四款120/128GB SSD横向评测
  12. XSS Reflected 测试
  13. namenode No valid image files
  14. java线程学习之join方法
  15. RANSAC介绍(Matlab版直线拟合+平面拟合)
  16. 在 Ionic2 TypeScript 项目中导入第三方 JS 库
  17. (转载)西门子PLC学习笔记十五-(数据块及数据访问方式)
  18. (转载)WinformGDI+入门级实例——扫雷游戏(附源码)
  19. tomcat监控脚本(监控进程,测试接口,告警动作为发送邮件)
  20. Oracle EBS AR 客户API

热门文章

  1. Android 汉字转拼音之JNI篇
  2. html+css基础
  3. while 、do...while 、for
  4. .net mvc笔记1_ The MVC Pattern
  5. Linux ---&gt; 监控JVM工具
  6. protel99与win7兼容问题的解决方案
  7. HDU 4416 Good Article Good sentence(后缀自动机)
  8. swith 语句详解
  9. Java加密解密与数字证书的操作
  10. unix more命令