1251_统计难题
???????????????????????????????????????????? Total Submission(s): 26607????Accepted Submission(s): 10774
Problem Description Ignatius最近遇到一個(gè)難題,老師交給他很多單詞(只有小寫(xiě)字母組成,不會(huì)有重復(fù)的單詞出現(xiàn)),現(xiàn)在老師要他統(tǒng)計(jì)出以某個(gè)字符串為前綴的單詞數(shù)量(單詞本身也是自己的前綴).
?
Input 輸入數(shù)據(jù)的第一部分是一張單詞表,每行一個(gè)單詞,單詞的長(zhǎng)度不超過(guò)10,它們代表的是老師交給Ignatius統(tǒng)計(jì)的單詞,一個(gè)空行代表單詞表的結(jié)束.第二部分是一連串的提問(wèn),每行一個(gè)提問(wèn),每個(gè)提問(wèn)都是一個(gè)字符串.
注意:本題只有一組測(cè)試數(shù)據(jù),處理到文件結(jié)束.
?
Output 對(duì)于每個(gè)提問(wèn),給出以該字符串為前綴的單詞的數(shù)量.
?
Sample Input banana band bee absolute acmba b band abc ?
Sample Output 2 3 1 0 ?
<span style="font-size:14px;">#include<cstdlib> #include<cstring> #include<iostream> using namespace std;struct node{ int cnt; struct node *next[26]; node() { cnt=0; memset(next,0,sizeof(next)); } };node *root=NULL;void buildtrie(char *s) { node *p=root; node *tmp=NULL; int i,l=strlen(s); for(i=0;i<l;i++) { if(p->next[s[i]-'a']==NULL) { tmp=new node; p->next[s[i]-'a']=tmp; } p=p->next[s[i]-'a']; p->cnt++; } } void findtrie(char *s) { node *p=root; int i,l=strlen(s); for(i=0;i<l;i++) { if(p->next[s[i]-'a']==NULL) { printf("0\n"); return; } p=p->next[s[i]-'a']; } printf("%d\n",p->cnt); } int main(){char ch1[12];root=new node;while(gets(ch1)&&ch1[0]!='\0'){//getchar();buildtrie(ch1);}while(scanf("%s",ch1)!=EOF){findtrie(ch1);} return 0; } </span>
??????????????????????????????????統(tǒng)計(jì)難題
???????????????????????????????Time Limit: 4000/2000 MS (Java/Others)????Memory Limit: 131070/65535 K (Java/Others)???????????????????????????????????????????? Total Submission(s): 26607????Accepted Submission(s): 10774
Problem Description Ignatius最近遇到一個(gè)難題,老師交給他很多單詞(只有小寫(xiě)字母組成,不會(huì)有重復(fù)的單詞出現(xiàn)),現(xiàn)在老師要他統(tǒng)計(jì)出以某個(gè)字符串為前綴的單詞數(shù)量(單詞本身也是自己的前綴).
?
Input 輸入數(shù)據(jù)的第一部分是一張單詞表,每行一個(gè)單詞,單詞的長(zhǎng)度不超過(guò)10,它們代表的是老師交給Ignatius統(tǒng)計(jì)的單詞,一個(gè)空行代表單詞表的結(jié)束.第二部分是一連串的提問(wèn),每行一個(gè)提問(wèn),每個(gè)提問(wèn)都是一個(gè)字符串.
注意:本題只有一組測(cè)試數(shù)據(jù),處理到文件結(jié)束.
?
Output 對(duì)于每個(gè)提問(wèn),給出以該字符串為前綴的單詞的數(shù)量.
?
Sample Input banana band bee absolute acmba b band abc ?
Sample Output 2 3 1 0 ?
<span style="font-size:14px;">#include<cstdlib> #include<cstring> #include<iostream> using namespace std;struct node{ int cnt; struct node *next[26]; node() { cnt=0; memset(next,0,sizeof(next)); } };node *root=NULL;void buildtrie(char *s) { node *p=root; node *tmp=NULL; int i,l=strlen(s); for(i=0;i<l;i++) { if(p->next[s[i]-'a']==NULL) { tmp=new node; p->next[s[i]-'a']=tmp; } p=p->next[s[i]-'a']; p->cnt++; } } void findtrie(char *s) { node *p=root; int i,l=strlen(s); for(i=0;i<l;i++) { if(p->next[s[i]-'a']==NULL) { printf("0\n"); return; } p=p->next[s[i]-'a']; } printf("%d\n",p->cnt); } int main(){char ch1[12];root=new node;while(gets(ch1)&&ch1[0]!='\0'){//getchar();buildtrie(ch1);}while(scanf("%s",ch1)!=EOF){findtrie(ch1);} return 0; } </span>
總結(jié)
- 上一篇: 微信开发源码讲解
- 下一篇: JEECG 开创新开发模式(即将发布版本