Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).
注意:本题只有一组测试数据,处理到文件结束.
banana band bee absolute acm ba b band abc
2 3 1 0
...大佬思路
#include <stdio.h>
#include <string.h>
#include <map>
#include <string>
using namespace std;
map<string,int>m1;
int main()
{
char z[10];
while(gets(z))
{
if(strlen(z)==0)
break;
for(int i=strlen(z);i>0;i--)
{
z[i]='\0';
m1[z]++;//记录z串出现了几次
}
}
while(gets(z))
{
printf("%d\n",m1[z]);
}
return 0;
}