[字典树] HDU 1804 - Deli Deli
来源:互联网 发布:arma预测c 源码 编辑:程序博客网 时间:2024/06/08 16:40
字典树裸题,每次插入完毕后,把这个字符串对应的串存到当前结点里。
查询的时候即可。。再特殊处理一下没在字典树里的其他情况,y前是辅音才转换为ies。。
因为这个WA了一次。。
#include <stdio.h>#include <string.h>#include <math.h>#include <stdlib.h>#include <algorithm>#include <iostream>#include <set>#include <map>#include <queue>#include <stack>#include <assert.h>#include <time.h>typedef long long LL;const int INF = 500000001;const double EPS = 1e-9;const double PI = acos(-1.0);using namespace std;struct Trie{ Trie *next[26]; bool flag; // 标记字符串结尾结尾 char *st; // 插入操作结束后,把对应的字符串放进去};Trie *H;Trie *newnode(){ Trie *p = new Trie; for(int i = 0; i < 26; i++) { p->next[i] = NULL; } p->flag = false; p->st = NULL;}int idx(char c){ return c - 'a';}void insert(char *ss, char *x){ Trie *p = H; int len = strlen(ss); for(int i = 0; i < len; i++) { int c = idx(ss[i]); if(p->next[c] == NULL) { p->next[c] = newnode(); } p = p->next[c]; } p->flag = true; p->st = new char[21]; strcpy(p->st, x);}int query(char *ss){ Trie *p = H; int len = strlen(ss); for(int i = 0; i < len; i++) { int c = idx(ss[i]); if(p->next[c] == NULL) { return 0; } p = p->next[c]; } if(p->flag) { puts(p->st); return 1; } return 0;}int main(){ //freopen("test0.in", "r", stdin); //freopen("test0.out", "w", stdout); //srand(time(NULL)); char flag[5][10]={"o", "s", "ch", "sh", "x"}; // 以这些结尾变es int vowel[26]={0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1}; // 元音、辅音标记数组 char *ss; //查找某个字符串的子串指针 int L, N; char ch1[100], ch2[100], ch3[100]; while(~scanf("%d %d", &L, &N)) { H = newnode(); for(int i = 0; i < L; i++) { scanf("%s %s", ch1, ch2); insert(ch1, ch2); } for(int i = 0; i < N; i++) { scanf("%s", ch3); int len = strlen(ch3); int flg = 0; if(!query(ch3)) { for(int j = 0; j < 5; j++) { ss = ch3; if(len < strlen(flag[j])) { continue; } while(ss = strstr(ss, flag[j])) { if(ss - ch3 == len - strlen(flag[j])) { flg = 1; strcat(ch3, "es"); break; } ss++; } } if(!flg) { if(ch3[len-1] == 'y' && len-2 >= 0 && vowel[ch3[len-2]]) { ch3[len-1] = 'i'; strcat(ch3, "es"); } else { strcat(ch3, "s"); } } printf("%s\n", ch3); } } } return 0;}
0 0
- [字典树] HDU 1804 - Deli Deli
- HDU 1804 Deli Deli
- HDU 1804 Deli Deli
- HDU 1804:Deli Deli
- HDU——1804 Deli Deli
- HDU 1804 Deli Deli (String)
- HDU_1804_Deli Deli (字典树)
- HDOJ/HDU 1804 Deli Deli(英语单词复数形式~)
- Deli Deli
- poj 3366 Deli Deli
- hdu_1804 Deli Deli
- HDU1804 Deli Deli
- UVa 11233 - Deli Deli
- BNUOJ 1010 Deli Deli
- HDU_1804Deli Deli
- 【TOJ 2911】 Deli Deli【水题】
- POJ 3366 Deli Deli 可能会
- 【端午小练】HDU1804-Deli Deli
- Windows7自带防火墙使用攻略
- 第十二周项目二 分离正整数中的各位数
- 守护进程
- [小技巧] outlook 里追加附件的快捷键
- 第十二周项目二——分离正整数的各位数
- [字典树] HDU 1804 - Deli Deli
- Android GridView去掉四周间隙
- 结构体之位域
- java jar包加密
- 第十二周 项目二:OJ平台中多种输入形式的处理(3)以某特殊输入为结束标志
- 第12周项目3-用递归方法求解(2)
- 成都催乳师培训 2010年
- 找出链表的第一个公共结点 【微软面试100题 第六十二题】
- 在页面上通过js调用exe程序