日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

统计难题(HDU-1251)

發布時間:2025/3/17 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 统计难题(HDU-1251) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Problem Description

Ignatius最近遇到一個難題,老師交給他很多單詞(只有小寫字母組成,不會有重復的單詞出現),現在老師要他統計出以某個字符串為前綴的單詞數量(單詞本身也是自己的前綴).?

Input

輸入數據的第一部分是一張單詞表,每行一個單詞,單詞的長度不超過10,它們代表的是老師交給Ignatius統計的單詞,一個空行代表單詞表的結束.第二部分是一連串的提問,每行一個提問,每個提問都是一個字符串.?

注意:本題只有一組測試數據,處理到文件結束.?

Output

對于每個提問,給出以該字符串為前綴的單詞的數量.?

Sample Input

banana
band
bee
absolute
acm

ba
b
band
abc

Sample Output

2
3
1
0

思路:字典樹入門題

Source Program

#include<iostream> #include<cstdio> #include<cstdlib> #include<string> #include<cstring> #include<cmath> #include<ctime> #include<algorithm> #include<utility> #include<stack> #include<queue> #include<vector> #include<set> #include<map> #define PI acos(-1.0) #define E 1e-9 #define INF 0x3f3f3f3f #define N 500001 #define LL long long const int MOD=20091226; const int dx[]= {-1,1,0,0}; const int dy[]= {0,0,-1,1}; using namespace std; int trie[N][26]; int tot; int sum[N]; void insert(char *s){int root=0;int len=strlen(s);for(int i=0;i<len;i++){int id=s[i]-'a';if(!trie[root][id])trie[root][id]=++tot;sum[trie[root][id]]++;root=trie[root][id];} } int search(char *s){int root=0;int len=strlen(s);for(int i=0;i<len;i++){int id=s[i]-'a';if(!trie[root][id])return 0;root=trie[root][id];}return sum[root]; } int main(){char str[20];while(gets(str)){if(strlen(str)==0)break;insert(str);}while(gets(str)){printf("%d\n",search(str));}return 0; }

?

總結

以上是生活随笔為你收集整理的统计难题(HDU-1251)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。