hdu 1251统计难题
来源:互联网 发布:华云数据集团 编辑:程序博客网 时间:2024/04/28 03:20
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1251
字典树模版题
动态实现:
#include<stdio.h>#include<stdlib.h>#include<string.h>#define maxn 26struct node{int count;node *next[maxn];}*root;void insert(char str[]){int i,len = strlen(str);node *current,*newset;current = root;for(i = 0; i < len; i++){int k = str[i] - 'a';if(current->next[k] != NULL){current = current->next[k];current->count++;}else{newset = (node*)malloc(sizeof(node));memset(newset->next,NULL,sizeof(newset->next));current->next[k] =newset;current = newset;current->count = 1;}}}int find(char str[]){int i,len = strlen(str);if(len == 0)return 0;node *current = root;for(i = 0; i < len ; i++){int k = str[i] - 'a';if(current->next[k] != NULL)current = current->next[k];elsereturn 0;}return current->count;}int main(){char str[101];root = (node*)malloc(sizeof(node));memset(root->next,NULL,sizeof(root->next));while(gets(str) && str[0] != '\0')insert(str);while(~scanf("%s",str))printf("%d\n",find(str));return 0;}
静态实现:
#include<stdio.h>#include<string.h>struct node{int cnt;node *next[26];node(){cnt = 0;memset(next,NULL,sizeof(next));}}tree[400000];int cnt = 1;node *root;void insert(char *str){node *p = root;int m;while(*str){m = *str- 'a';if(p->next[m] == NULL)p->next[m] = &tree[cnt++];p = p->next[m];p->cnt ++;str++;}}int search(char *str){int m;node *p = root;while(*str){m = *str - 'a';if(p->next[m] == NULL)return 0;p = p->next[m];str++;}return p->cnt;}int main(){char str[15];root = tree;cnt = 1;while(gets(str) && str[0] != '\0')insert(str);while(~scanf("%s",str))printf("%d\n",search(str));return 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 统计难题
- 功率分配器相关技术指标
- POJ 3256(SPFA)
- 【第2天】态度决定一切
- VC 利用DLL共享区间在进程间共享数据及进程间广播消息
- Failed to fectch URl https://dl-ssl.google.com/android/repository/addons_list.xml, reason: Connectio
- hdu 1251统计难题
- Javascript中最常用的55个经典技巧
- 编程困难没思路,我差到哪儿了?
- amr的相关资料(1)
- SAP《MM学习指南》操作记录----仓库盘点
- C语言-- gets()和scanf()函数的区别
- SAP 设置或取消仓库不参与MRP运算
- Java性能监控系列——java.lang.instrument
- 4×4矩阵键盘的工作原理与编程