解题思路:
1思路:暴力+map
2分析:建立一个char 数组用来存储每一个字母对应的数值。然后求出每一个单词的序列,然后利用map查找。最后输出
3注意:如果是采取每一次都去搜索那么肯定TLE
代码:
#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <map> using namespace std; #define MAXN 5010 int t, n, m; char ch[26] = {'2', '2', '2', '3', '3', '3', '4', '4', '4', '5', '5', '5', '6', '6', '6', '7', '7', '7', '7', '8', '8', '8', '9', '9', '9', '9'}; string word[MAXN]; string str[MAXN]; map<string , int>mp; void solve() { map<string , int>::iterator it; for (int i = 0; i < m; i++) { cin >> word[i]; string s = ""; for(int j = 0 ; j < word[i].size() ; j++) s += ch[word[i][j]-'a']; it = mp.find(s);/*查找*/ if(it != mp.end()) mp[s]++; else mp[s] = 1; } for(int i = 0 ; i < n ; i++) printf("%d\n" , mp[str[i]]-1);/*这里要减1*/ } int main() { //freopen("input.txt" , "r" , stdin); scanf("%d", &t); while (t--) { mp.clear(); scanf("%d%d", &n, &m); for (int i = 0; i < n; i++){ cin>>str[i]; mp[str[i]] = 1;/*插入map容器*/ } solve(); } }