hud-1247-Hat’s Words
来源:互联网 发布:linux英文改中文 编辑:程序博客网 时间:2024/04/29 17:12
这道题目是字典树的典型题目。
字典树详见链接:http://www.cnblogs.com/pony1993/archive/2012/07/18/2596730.html
令人很郁闷的是
把输入改成 while(scanf("%s",ch)!=EOF) 就AC了……用 while(gets(ch)!=NULL) 一直WA……;
代码:
#include<iostream>#include<cstring>#include<malloc.h>using namespace std;#define num 5000001char st[num][101];struct list{ int leap; struct list *next[26];};struct list *creat(){ int i; struct list *p; p=(struct list *)malloc(sizeof(struct list)); p->leap=0; for(i=0;i<26;i++) { p->next[i]=NULL; } return p;}void in(char *str,struct list *q){ int i,n,t; n=strlen(str); struct list *p=q; for(i=0;i<n;i++) { t=str[i]-'a'; if(p->next[t]==NULL) { p->next[t]=creat(); } p=p->next[t]; } p->leap=1;}int search(char *str,struct list *q){ int i,n,t; struct list *p=q; n=strlen(str); for(i=0;i<n;i++) { t=str[i]-'a'; if(p->next[t]==NULL) return 0; p=p->next[t]; } return p->leap;}int main(){ char s1[101],s2[101]; int i,j,k,q=0,l; struct list *tree; tree=creat(); while(scanf("%s%*c", st[q])!=EOF) { in(st[q],tree); q++; } for(i=0;i<q;i++) { memset(s1,0,sizeof(s1)); l=strlen(st[i]); { for(j=0;j<l;j++) { s1[j]=st[i][j]; if(search(s1,tree)) { memset(s2,0,sizeof(s2)); for(k=j+1;k<l;k++) { s2[k-j-1]=st[i][k]; } if(search(s2,tree)) { puts(st[i]); break; } } } } } return 0;}
- hud-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 Hat's Words(1247)
- HDU 1247 Hat’s Words
- HDU 1247 Hat’s Words
- acm竞赛要掌握的知识 & 北大ACM分类 & ACM网址大全
- setTimeout和setInterval区别 setTimeout模拟setInterval
- javac 的classpath用法小结
- 【eclipse】eclipse导入Dynamic Web Project项目
- c++主要知识点
- hud-1247-Hat’s Words
- iPhone delegate 两界面传递数据(转)
- C/C++中static关键字详解
- Epson LQ-630K打印机设置
- XMLWebService开发
- 【hdu】 Seaside (Floyd)注意初始化
- s3c6410 uboot代码分析
- android.database.sqlite.SQLiteException: database is locked
- 通过JDBC的DatabaseMetaData获取数据库元数据