码迷,mamicode.com
首页 > 其他好文 > 详细

字典树 HDU 1251 统计难题

时间:2015-07-31 09:02:37      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:

http://acm.hdu.edu.cn/showproblem.php?pid=1251

#include<iostream> #include<algorithm> #include<stdio.h>
using namespace std; struct node { int sum; node *next[26]; }; void buildtrietree(node *head, char s[]) { node *p=new node(); p=head; for(int i=0; s[i]; i++) { int k=s[i]-‘a‘; if(p->next[k]==0) p->next[k]=new node(); p=p->next[k]; p->sum++; } } int query(node *head, char s[]) { node *p=new node(); p=head; for(int i=0; s[i]; i++) { int k=s[i]-‘a‘; if(p->next[k]==0) return 0; p=p->next[k]; } return p->sum; } int main() { char s[12]; node *head=new node(); while(gets(s), s[0]) buildtrietree(head, s); while(cin >> s) printf("%d\n", query(head, s)); return 0; }

 

字典树 HDU 1251 统计难题

标签:

原文地址:http://www.cnblogs.com/wazqWAZQ1/p/4691142.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!