POJ 1318 Word Amalgamation
来源:互联网 发布:seo如何做外链 编辑:程序博客网 时间:2024/04/30 03:47
题目链接:http://poj.org/problem?id=1318
看了别人的代码觉得用了set数据结构真的好神奇, 好简单
题目大意:从字典中寻找由 一些字母组成的单词
代码:字典树,排序
思路:在字典的节点上保存下存入 经过排序的
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>using namespace std;struct Str{char s[7];};struct nod{int c[29];Str s[100];int cnt;int fl;}p[600];int tot = 0;char temp[8];int New_node(int n){memset(p[tot].c, -1, sizeof(p[tot].c));p[tot].cnt = 0;return tot ++ ;}bool cmp(char a, char b){return a < b ;}bool cmp1(Str a, Str b){return strcmp(a.s, b.s) < 0;}void insert(char s[]){int now = 0;int len = strlen(s);strcpy(temp, s);sort(s, s+len);int i;for(i = 0; i < len; i ++){int a = s[i] - 'a';if(p[now].c[a] == -1)p[now].c[a] = New_node(i+1);now = p[now].c[a];}strcpy(p[now].s[p[now].cnt].s, temp);p[now].cnt ++;}void find(char s[]){int now = 0;int len = strlen(s);sort(s, s+len);int i;for(i = 0; i < len; i ++){int a = s[i] - 'a';if(p[now].c[a] == -1){printf("NOT A VALID WORD\n");return ;}now = p[now].c[a];}sort(p[now].s, p[now].s + p[now].cnt, cmp1);for(i = 0; i < p[now].cnt; i ++)printf("%s\n", p[now].s[i].s);}int main(){int root = New_node(0);char s[12];while(scanf("%s", s)){if(s[0] == 'X')break;insert(s);}while(scanf("%s", s)){if(s[0] == 'X')break;find(s);printf("******\n");}return 0;}
单词,然后把节点所有的单词排序, 用find函数搜索到节点输出单词
- POJ 1318 Word Amalgamation
- POJ 1318 Word Amalgamation
- poj 1318 Word Amalgamation
- POJ 1318 Word Amalgamation
- poj 1318 Word Amalgamation
- poj 1318 Word Amalgamation
- poj 1318 Word Amalgamation
- POJ-1318 Word Amalgamation
- poj 1318(Word Amalgamation)
- POJ 1318 Word Amalgamation 水
- POJ 1318 Word Amalgamation 笔记
- POJ-1318-Word Amalgamation-STL的使用
- POJ 1318 Word Amalgamation (模拟)
- POJ 1318 Word Amalgamation (简单题)
- POJ 1318 Word Amalgamation (字符串 STL大水)
- poj 1318 Word Amalgamation map的使用
- POJ 1113 Word Amalgamation
- 1318 Word Amalgamation
- Sybase封锁原理(死锁)
- 使用Zipalign工具优化Android APK应用
- 各种流行的编程风格(转)
- pure-ftp的相关命令
- poj2135 最小费用最大流
- POJ 1318 Word Amalgamation
- C++复制
- 持续更新 iText in Action 2nd Edition中文版 个人翻译
- java事件机制
- Node.js开发指南之一:基本概念与开发环境的配置
- lucene 构建索引
- java中类加载路径和项目根路径获取的几种方式
- Eclipse插件spket安装
- cppcms application的理解