统计难题
http://acm.hdu.edu.cn/diy/contest_showproblem.php?cid=12654&pid=1001
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131070/65535K (Java/Other)
Total Submission(s) : 4 Accepted Submission(s) : 4
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).
Input
输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串.
注意:本题只有一组测试数据,处理到文件结束.
注意:本题只有一组测试数据,处理到文件结束.
Output
对于每个提问,给出以该字符串为前缀的单词的数量.
Sample Input
banana band bee absolute acm ba b band abc
Sample Output
2 3 1 0
Author
#include<iostream>
#include<cstring>
using namespace std;
struct trie{
struct trie *child[26];
int n; //经过这个点的单词数
};
struct trie *root;
void insert(char *source){
int len,i,j;
struct trie *current,*newnode;
len=strlen(source);
if(len==0) return ;
current=root;
for(i=0;i<len;i++){
if(current->child[source[i]-'a']!=NULL){
current=current->child[source[i]-'a'];
current->n ++; //经过这个点的单词数加1
}
else{
newnode = new trie;
for(j=0;j<26;j++)
newnode ->child[j]=NULL;
current->child[source[i]-'a']=newnode;
current=newnode;
current->n=1;
}
}
}
int find(char *source){
int i,len;
len=strlen(source);
if(len==0) return 0;
struct trie *current;
current=root;
for(i=0;i<len;i++){
if(current->child[source[i]-'a']!=NULL)
current=current->child[source[i]-'a'];
else
return 0;
}
return current->n;
}
int main(){
char temp[11];
int i;
root=new trie;
for( i=0;i<26;i++)
root->child[i]=NULL;
root->n=0; //根节点不存放字母
while(gets(temp),strcmp(temp,"")!=0)
insert(temp);
while(scanf("%s",temp)!=EOF){
i=find(temp);
printf("%d\n",i);
}
return 0;
}
#include<cstring>
using namespace std;
struct trie{
struct trie *child[26];
int n; //经过这个点的单词数
};
struct trie *root;
void insert(char *source){
int len,i,j;
struct trie *current,*newnode;
len=strlen(source);
if(len==0) return ;
current=root;
for(i=0;i<len;i++){
if(current->child[source[i]-'a']!=NULL){
current=current->child[source[i]-'a'];
current->n ++; //经过这个点的单词数加1
}
else{
newnode = new trie;
for(j=0;j<26;j++)
newnode ->child[j]=NULL;
current->child[source[i]-'a']=newnode;
current=newnode;
current->n=1;
}
}
}
int find(char *source){
int i,len;
len=strlen(source);
if(len==0) return 0;
struct trie *current;
current=root;
for(i=0;i<len;i++){
if(current->child[source[i]-'a']!=NULL)
current=current->child[source[i]-'a'];
else
return 0;
}
return current->n;
}
int main(){
char temp[11];
int i;
root=new trie;
for( i=0;i<26;i++)
root->child[i]=NULL;
root->n=0; //根节点不存放字母
while(gets(temp),strcmp(temp,"")!=0)
insert(temp);
while(scanf("%s",temp)!=EOF){
i=find(temp);
printf("%d\n",i);
}
return 0;
}
下面是百度的一个代码,速度比上面的快了2倍。
#include<stdio.h>
struct tree{
bool isword;
int child[26],cnt;
}T[1000001];
char word[11];
int L = 1,ans;//树的总长
void Ins(char *a,int k,int idx)//k代表单词长度,也是深度,idx代表树的ID
{
if(!a[k])//这个单词建完
return ;
if(T[idx].child[a[k] - 'a'])//找到这个单词,继续往下找
{
T[ T[idx].child[ a[k] -'a'] ].cnt ++ ;
Ins(a,k+1,T[idx].child[a[k]-'a']);
}
else
{
T[idx].child[a[k]-'a'] = ++L;//树的总长++
T[L].isword = false;
T[L].cnt ++;
Ins(a,k+1,L);
}
}
void find(char *a ,int k,int idx)
{
if(!a[k])
{
ans = T[idx].cnt;//全部找完,返回
return ;
}
if(T[idx].child[a[k]-'a'] == 0)//找不到字母了,一定不是前缀
return ;
find(a,k+1,T[idx].child[a[k]-'a']);//继续找下一个字母
}
int main()
{
T[1].isword = false;
while (gets(word),word[0])
Ins(word,0,1);
while (gets(word))
{
ans = 0;
find(word,0,1);
printf("%d\n",ans);
}
return 0;
}
struct tree{
}T[1000001];
char word[11];
int L = 1,ans;//树的总长
void Ins(char *a,int k,int idx)//k代表单词长度,也是深度,idx代表树的ID
{
}
void find(char *a ,int k,int idx)
{
}
int main()
{
}
876

被折叠的 条评论
为什么被折叠?



