统计难题
来源:互联网 发布:幺正矩阵 编辑:程序博客网 时间:2024/05/22 06:58
统计难题
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submission(s): 22521 Accepted Submission(s): 9488
注意:本题只有一组测试数据,处理到文件结束.
bananabandbeeabsoluteacmbabbandabc
2310
Ignatius.L | We have carefully selected several similar problems for you: 1247 1298 2846 2222 1305
题意:给出一个单词表,上面存有一些单词,然后下面是一些前缀字符串,分别求出前缀字符串出现的次数。
分析:典型的字典树,但是也可以用map做。
代码一(字典树):
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
char str[11];
struct node{
node *next[26];
int cnt;//表示一颗字典树到此有多少颗前缀
node(){//构造函数,创建节点时自动执行
cnt=0;
for(int i=0;i<26;i++)
{
next[i]=NULL;//将该节点的下面的26个及诶单初始化为空
}
}
};
void insert(node *p,char *str)
{
for(int i=0;str[i];i++)
{
int t=str[i]-'a';
if(p->next[t]==NULL)
p->next[t]=new node; //如果节点不存在,先new一个
p=p->next[t];//指针指向子孩子
p->cnt++;//计数器累加
}
}
int find(node *p,char *str)
{
for(int i=0;str[i];i++)
{
int t=str[i]-'a';
p=p->next[t];//指针指向子孩子
if (!p)
return 0;
}
return p->cnt;
}
int main()
{
node *root= new node();
while(gets(str)&&strlen(str))
{
insert(root,str);
}
while(gets(str))
{
int ans=find(root,str);
printf("%d\n",ans);
}
return 0;
}
代码二(map):
#include<cstdio>
#include<cstring>
#include<iostream>
#include<map>
#include<algorithm>
using namespace std;
map<string,int>M;
int main()
{
string x;
char a;
while(true)
{
scanf("%c",&a);
if(a=='\n') //需要注意输入格式
{
scanf("%c",&a);
x="";
}
if(a=='\n')
break;
x+=a;
M[x]+=1;
}
while(cin>>x)
{
cout<<M[x]<<endl;
}
return 0;
}
- 统计难题
- 统计难题
- 统计难题
- 统计难题
- 统计难题
- 统计难题
- 统计难题
- 统计难题
- 统计难题
- 统计难题
- 统计难题
- 统计难题
- 统计难题
- 统计难题
- 统计难题
- 统计难题
- 统计难题
- 统计难题
- hdu 5288 OO’s Sequence 枚举因数
- 减小新版Lazarus编译的可执行文件的体积
- 记最近遇到的几个小问题(字符串转整型溢出,汉字乱码,双网卡双默认IP)
- 第四章
- 程序员必须知道的10大基础算法及讲解
- 统计难题
- js关联数组
- lucene4.5简单实例
- leetCode 79.Word Search (词搜索) 解题思路和方法
- imageloader详解
- 大数据,云计算,物联网和移动互联网关系图解
- java即时通信,推送技术详解
- poj 1011 Sticks 经典dfs+剪枝
- HDU 5289 Assignment (RMQ+二分)