HDU 1247 Hat's words(Trie)
来源:互联网 发布:淘宝黑搜技术 编辑:程序博客网 时间:2024/05/19 02:21
HDU 1247 Hat's words(Trie)
ACM
题目地址:
HDU 1247 Hat's words
题意:
给些单词,问每个单词是否能用另外两个单词拼出。
分析:
直接保存到trie里面,然后暴力切割查询即可。
代码:
/** Author: illuz <iilluzen[at]gmail.com>* File: 1247.cpp* Create Date: 2014-09-24 11:04:11* Descripton: */#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define repf(i,a,b) for(int i=(a);i<=(b);i++)typedef long long ll;const int N = 50010;const int MAXNODE = 1000010;const int MAXSON = 26;// array indexstruct ATrie {int ch[MAXNODE][MAXSON];int val[MAXNODE];int sz;// num of nodesATrie() { sz = 1; memset(ch[0], 0, sizeof(ch[0])); }void init() { sz = 1; memset(ch[0], 0, sizeof(ch[0])); }inline int idx(char c) { return c ? c - 'a' : 0; }void insert(char *s, int v = 1) {int u = 0, len = strlen(s);repf (i, 0, len - 1) {int c = idx(s[i]);if (!ch[u][c]) {memset(ch[sz], 0, sizeof(ch[sz]));val[sz] = 0;ch[u][c] = sz++;}u = ch[u][c];}val[u] = v;}// if s in trie return the value, else return 0int find(char *s) {int u = 0, len = strlen(s);repf (i, 0, len - 1) {int c = idx(s[i]);if (ch[u][c])u = ch[u][c];elsereturn 0;}return val[u];}} trie;char wd[N][110], a[110], b[110];int n;int main() {// ios_base::sync_with_stdio(0);while (gets(wd[n])) {trie.insert(wd[n++]);}repf (i, 0, n - 1) {int len = strlen(wd[i]);repf (j, 1, len - 1) {strncpy(a, wd[i], j);a[j] = 0;strcpy(b, wd[i] + j);// cout << a << ' ' << b << endl;if (trie.find(a) && trie.find(b)) {printf("%s\n", wd[i]);break;}}}return 0;}
0 0
- HDU 1247 Hat's words(Trie)
- hdu 1247 Hat’s Words(Trie)
- HDU 1247 Hat’s Words (Trie)
- hdu 1247 Hat’s Words Trie树
- HDU 1247 Hat’s Words Trie题解
- hdu 1247 Hat’s Words(dfs+trie)
- HDU -- 1247 Hat’s Words (Trie 树)
- hdu 1247 Hat’s Words trie
- HDU 1247 Hat’s Words // Trie, 枚举
- [hdu 1247]Hat’s Words [Trie 图]
- Hdu 1247 Hat's Words(Trie树)
- Hat’s Words(hdu 1247)(trie tree)
- HDU 1247 Hat’s Words && Trie(字典树)
- HDU ACM 1247-Hat’s Words-字典树(Trie)
- hdu 1247 Hat’s Words(Trie树入门)
- hdu 1247 Hat’s Words Trie树(+测试数据)
- HDU 1247-Hat’s Words(trie树)
- HDU-1247 Hat’s Words (Trie 字典树)
- MySQL外键的使用
- cocos2d-x 聊天输入框实现
- Hibernate 4.3.6 构建SessionFactory的方式
- UF超滤膜:超滤膜组件技术优势
- 关于“团队建设”的反思
- HDU 1247 Hat's words(Trie)
- FilterDispatcher与StrutsPrepareAndExecuteFilter
- JQuery中$.ajax()方法参数详解
- Android 自定义Dialog时出现成员变量为null的问题
- ios中KeyChain用途
- Java读取文件方法大全
- Divide Two Integers
- POM.xml 3
- 游戏开发常用的排序算法