您的位置:首页 > 其它

华为OJ中级题-查找兄弟单词

2015-12-08 08:59 357 查看
bool findBrother(string x,string y){
int len = x.length();
if (len != y.length()){ return false; }
if (x == y){ return false; }

int book[256] = { 0 }, book2[256] = {0};
for (int i = 0; i < len; ++i){
++book[x[i]];
}

int c = 0;
for (int i = 0; i < len; ++i){
for (int j = 0; j < len; ++j){
if (x[i] == y[j]&&book2[y[j]]<book[x[j]]){ ++c; ++book2[y[j]]; }
}
}

if (c == len)return true;
else return false;
}
void HWoj(){
int N,n;
vector<string> words,outStr;
string tmp;
cin >> N;
while (N--){
cin >> tmp;
words.push_back(tmp);
}
cin >> tmp;
cin >> n;
int lens = words.size();
int count=0;
int i = 0;
for (i; i < lens; ++i){
if (findBrother(tmp, words.at(i))){ ++count; outStr.push_back(words.at(i)); }
}
lens = outStr.size();
cout << count << endl;
sort(outStr.begin(), outStr.end());
cout << outStr.at(n-1) << endl;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: