華為筆試-查詢兄弟單詞
阿新 • • 發佈:2018-12-19
輸入描述:
先輸入字典中單詞的個數,再輸入n個單詞作為字典單詞。 輸入一個單詞,查詢其在字典中兄弟單詞的個數 再輸入數字n
輸出描述:
根據輸入,輸出查詢到的兄弟單詞的個數
示例1
輸入
3 abc bca cab abc 1
輸出
2 bca
程式碼如下
//Q26查詢兄弟單詞 class Dic { public: }; int Q26BrotherWordsFind() { int n = 0; while(cin >> n) { vector<string> vecstr; string str; for(unsigned int i = 0; i < n; i++) { cin>>str; vecstr.push_back(str); } string goal; cin >> goal; int k = 0; cin >> k; vector<string> brostr; sort(vecstr.begin(),vecstr.end()); for (unsigned int i = 0; i < vecstr.size(); i++) { if(goal.size() != vecstr[i].size()) continue; if(goal == vecstr[i]) continue; string sgoal = goal; string sstr = vecstr[i]; sort(sgoal.begin(),sgoal.end()); sort(sstr.begin(),sstr.end()); if(sstr == sgoal) brostr.push_back(vecstr[i]); } cout << brostr.size(); if(k < brostr.size()) cout << " " << brostr[k]; cout << endl; } return 0; }