hdu-1251
来源:互联网 发布:金融it 编辑:程序博客网 时间:2024/06/05 10:28
思路:
Trie树模板的小变形,在模板中有一个思维拓展的点要值得我们注意,就是每一个节点的e值,在本题中他们不再用来标记单词的结尾,而是用来计数,因为对于Trie树的某一个确定的位置,一个给定的单词只能走过一遍,因此这样记录可以确定一个位置被多少个单词给“经过”,然后找前缀的时候只要遍历到的最后一个字母所在的位置,输出他的count即可。
AC代码:
#include <iostream>#include <cstring>#include <cstdio>using namespace std;struct node{ int e; struct node* next[26]; node() { e = 0; for(int i = 0;i <= 25;i++) next[i] = NULL; }};node* root;char str[100007][17];char cs[100007][17];void Insert(char* s){ node* p = root; for(;*s != '\0';s++) { int n = *s-'a'; if(p->next[n] == NULL) p->next[n] = new node(); p = p->next[n]; p->e++; }}int find(char* s){ node* p = root; for(;*s!='\0';s++) { int n = *s-'a'; if(p->next[n] == NULL) return 0; p = p->next[n]; } return p->e;}int main(){ int i,j; i = 0; j = 0; root = new node(); while(gets(str[i]),strcmp(str[i],"")) Insert(str[i++]); while(scanf("%s",cs[j]) != EOF) cout<<find(cs[j++])<<endl; return 0;}
0 0
- hdu 1251
- hdu 1251
- HDU 1251
- hdu 1251
- HDU-1251
- HDU 1251
- hdu 1251
- hdu-1251
- hdu 1251
- HDU 1251
- HDU 1251
- hdu 1251
- HDU 1251
- hdu 1251
- hdu 1251
- hdu 1251
- hdu-1251
- HDU 1251
- hdu2444The Accomodation of Students
- Alljoyn瘦客户端库介绍
- 基础知识整理
- hdu-1247
- 指针和引用的参数传递
- hdu-1251
- iOS开发之数据存储
- hdu-2846 Repository
- 字符串的空格替换
- hdu-4287 Intelligent IME
- hdu-1800
- 动态规划中的0-1背包模型
- CF#345 div2 A\B\C题
- C++中输入输出流运算符的重载