HDU 1251 统计难题 字典树/STL
生活随笔
收集整理的這篇文章主要介紹了
HDU 1251 统计难题 字典树/STL
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
統(tǒng)計難題 Time Limit:2000MS?????Memory Limit:65535KB?????64bit IO Format:%I64d & %I64u
注意:本題只有一組測試數(shù)據(jù),處理到文件結束.?
Description
Ignatius最近遇到一個難題,老師交給他很多單詞(只有小寫字母組成,不會有重復的單詞出現(xiàn)),現(xiàn)在老師要他統(tǒng)計出以某個字符串為前綴的單詞數(shù)量(單詞本身也是自己的前綴).?Input
輸入數(shù)據(jù)的第一部分是一張單詞表,每行一個單詞,單詞的長度不超過10,它們代表的是老師交給Ignatius統(tǒng)計的單詞,一個空行代表單詞表的結束.第二部分是一連串的提問,每行一個提問,每個提問都是一個字符串.?注意:本題只有一組測試數(shù)據(jù),處理到文件結束.?
Output
對于每個提問,給出以該字符串為前綴的單詞的數(shù)量.?Sample Input
banana band bee absolute acm ba b band abcSample Output
2 3 1 0 第一種是普通的字典樹做法 網(wǎng)上找了個模板 ?指針這東西真是玄學啊? #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <queue> #include <vector> using namespace std; #define INF 0x3f3f3f3f #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1typedef long long LL;struct node {int count;node *childs[26];node(){count=0;for(int i=0;i<26;i++)childs[i]=NULL;} };node *root=new node; node *current,*newnode;void insert(char *str) {int m;int len=strlen(str);current=root;for(int i=0;i<len;i++){m=str[i]-'a';if(current->childs[m]!=NULL){current=current->childs[m];++(current->count);}else{newnode=new node;++(newnode->count);current->childs[m]=newnode;current=newnode;}} }int search(char *str) {int m;current=root;int len=strlen(str);for(int i=0;i<len;i++){m=str[i]-'a';if(current->childs[m]==NULL)return 0;current=current->childs[m];}return current->count; }int main() {//freopen("input.txt","r",stdin);char str[26];while(gets(str)){int len=strlen(str);if(len==0)break;insert(str);}while(gets(str)!=NULL){printf("%d\n",search(str));} }
?
然后再來說說神奇的map
仰望高端玄學
#include <iostream> #include <cstdio> #include <map> #include <string.h> using namespace std;char str[25];int main() {//freopen("input.txt","r",stdin);map<string,int> m;while(gets(str)){int len=strlen(str);if(len==0)break;for(int i=len;i>0;i--){str[i]='\0';m[str]++;}}while(gets(str)){printf("%d\n",m[str]);} }
?
轉(zhuǎn)載于:https://www.cnblogs.com/Hyouka/p/5716467.html
總結
以上是生活随笔為你收集整理的HDU 1251 统计难题 字典树/STL的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PCM转MP3工具的封装
- 下一篇: 元素,布局方式,BFC和清除浮动