请设计一个高效算法,再给定的字符串数组中,找到包含”Coder”的字符串(不区分大小写),并将其作为一个新的数组返回。结果字符串的顺序按照”Coder”出现的次数递减排列,若两个串中”Coder”出现的次数相同,则保持他们在原数组中的位置关系。
给定一个字符串数组A和它的大小n,请返回结果数组。保证原数组大小小于等于300,其中每个串的长度小于等于200。同时保证一定存在包含coder的字符串。
样例输入:[“i am a coder”,”Coder Coder”,”Code”],3
输出:返回:[“Coder Coder”,”i am a coder”]
C++源代码:
// Qunar problem 2017.03.24 // 2017.03.24 by wyj /* 名企笔试:去哪儿2015研发笔试题(寻找Coder)2017-03-24 算法爱好者 请设计一个高效算法,再给定的字符串数组中,找到包含”Coder”的字符串(不区分大小写), 并将其作为一个新的数组返回。结果字符串的顺序按照”Coder”出现的次数递减排列, 若两个串中”Coder”出现的次数相同,则保持他们在原数组中的位置关系。 给定一个字符串数组A和它的大小n,请返回结果数组。 保证原数组大小小于等于300,其中每个串的长度小于等于200。 同时保证一定存在包含coder的字符串。 样例输入: [“i am a coder”,”Coder Coder”,”Code”],3 输出: 返回:[“Coder Coder”,”i am a coder”] */ #include #include #include using namespace std; class Str { public: string A; int count; }; Str si[300]; //输入的字符串 Str so[300]; //输出的字符串 Str temp; int main() { int n, i = 0, j = 0, k = 0; cin >> n; //输入字符串个数 while (getchar() != '\n'); for (i = 0;i < n;i++) //输入字符串 getline(cin, si[i].A); for (i = 0;i < n;i++) { int len = si[i].A.length(); for (j = 0;j < k;j++) if (so[k-1].count > so[j].count) { temp = so[j]; so[j] = so[k-1]; so[k-1] = temp; } } } for (i = 0;i < k;i++) //输出结果 cout << so[i].A << endl << "找到" << so[i].count << "个coder。" << endl; return 0; }
名企笔试:去哪儿2015研发笔试题(寻找Coder)2017-03-24 算法爱好者
最新推荐文章于 2019-03-24 16:08:00 发布