首页 > 学院 > 开发设计 > 正文

华为OJ:查找兄弟单词

2019-11-08 02:15:12
字体:
来源:转载
供稿:网友

查找兄弟单词

输入描述: 先输入字典中单词的个数,再输入n个单词作为字典单词。 输入一个单词,查找其在字典中兄弟单词的个数 再输入数字n

输出描述: 根据输入,输出查找到的兄弟单词的个数

输入例子: 3 abc bca cab abc 1

输出例子: 2 bca

#include <cstdio>#include <string>#include <vector>#include <iostream>#include <algorithm>using namespace std;vector<string> dict;bool is_friend(string a, string b){ if (a == b) return false; sort(a.begin(), a.end()); sort(b.begin(), b.end()); return a == b;}int main(){ int n; while (scanf("%d", &n) == 1) { dict.clear(); for (int i = 0; i < n; i++) { string s; cin >> s; dict.push_back(s); } sort(dict.begin(), dict.end()); string ask; cin >> ask; int id; scanf("%d", &id); int cnt = 0; string res = ""; for (int i = 0; i < (int)dict.size(); i++) { if (is_friend(dict[i], ask)) { cnt++; if (cnt == id) { res = dict[i]; } } } PRintf("%d/n", cnt); if (cnt >= id) printf("%s/n", res.c_str()); } return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表