您的位置:首页 > 其它

Hdu Excuses, Excuses! 字符串处理 map应用

2013-07-27 15:43 260 查看
给出多组字符串,拿字符串里的单词去比较题目给出的keyword,哪个字符串里存在keyword里的单词最多,就输出,若有多个满足,输出它们

用map,不得不说map真的很神奇

代码:

#include <iostream>
#include <cstring>
#include <map>
#include <cmath>
#include <stack>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <string>
using namespace std;
struct node
{
char str[100];
int num;
}a[25];
bool cmp(node a,node b)
{
return a.num>b.num;
}
int main()
{
int n,m,cas=1;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(a,0,sizeof(a));
map<string,int>mp;
string s;
for(int i=0;i<n;i++)
{
cin>>s;
mp[s]=1;
}
getchar();
for(int i=0;i<m;i++)
{
gets(a[i].str);
int l=strlen(a[i].str);
string w="";
char b[100];
strcpy(b,a[i].str);
for(int j=0;j<l;j++)
{
if(b[j]>='A' && b[j]<='Z')
{
b[j]+=32;

}
}
for(int j=0;j<l;j++)
{
if(b[j]>='a'&&b[j]<='z')
w+=b[j];
else
{
map<string,int>::iterator it;
it=mp.find(w);
if(it!=mp.end())
a[i].num++;
w="";
}
}
}
sort(a,a+m,cmp);
int vv=a[0].num;
printf("Excuse Set #%d\n",cas);
cas++;
for(int i=0;i<m;i++)
{
if(a[i].num==vv)
{
printf("%s\n",a[i].str);
}
else
break;
}
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: