banana band bee absolute acm ba b band abc
2 3 1 0
简单hash,去每一个字符串的第一个字母减去‘a’,作为线索
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
const int M = 26;
struct node{
char s[15];
bool operator < (const node &a) const { //重载,就是在插入vector时排一下序
if(strcmp(s, a.s) <= 0) return 1;
else return 0;
}
};
vector<node> d[M];
int main(){
for(int i = 0; i < 26; ++ i) d[i].clear();
char s[25];
node temp;
while(gets(temp.s), temp.s[0] != '\0'){
d[temp.s[0]-'a'].push_back(temp);
}
/*for(int i = 0; i < 26; i ++){
for(int j = 0; j < d[i].size(); ++ j)
cout << d[i][j].s<<" ";
cout <<endl;
}*/
while(scanf("%s", s) == 1){
int ans = 0, j;
int sss = s[0]-'a';
for(int i = 0; i < d[sss].size(); ++ i){
for(j = 0; j < strlen(s); ++ j){
if(s[j] != d[sss][i].s[j]) break;
}
if(j == strlen(s)) ++ans;
}
printf("%d\n", ans);
}
return 0;
}原文:http://blog.csdn.net/shengweisong/article/details/44539265