//
// main.cpp
// newcoder
//
// Created by 吴珝君 on 2019/5/5.
// Copyright © 2019年 闲着也是贤者. All rights reserved.
//
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
/*
请设计一个高效算法,再给定的字符串数组中,找到包含"Coder"的字符串(不区分大小写),并将其作为一个新的数组返回。结果字符串的顺序按照"Coder"出现的次数递减排列,若两个串中"Coder"出现的次数相同,则保持他们在原数组中的位置关系。
给定一个字符串数组A和它的大小n,请返回结果数组。保证原数组大小小于等于300,其中每个串的长度小于等于200。同时保证一定存在包含coder的字符串。
测试样例:
["i am a coder","Coder Coder","Code"],3
返回:["Coder Coder","i am a coder"]
*/
class info_
{
public:
info_(int id, int count)
{
this->id = id;
this->count = count;
}
int id;
int count;
};
bool compare(info_ a, info_ b)
{
return a.count > b.count;
}
class Coder {
public:
vector<string> findCoder(vector<string> A, int n)
{
// write code here
vector<info_> v;
vector<string> vi;
for (int i =0; i < A.size(); i++)
{
int t = 0;
splitString(A[i], "CODER", t);
if (t >0)
{
// cout << i<<endl;
v.push_back(info_(i, t));
}
}
stable_sort(v.begin(), v.end(), compare);
for (int i =0; i < v.size(); i++) {
cout << A[v[i].id]<<"___id__" << v[i].id << endl;
vi.push_back(A[v[i].id]);
}
return vi;
}
private:
void splitString(const string &s, string c , int &count)
{
string str = s;
transform(str.begin(), str.end(),str.begin() , ::toupper);
//cout << str<<endl;
string::size_type pos1, pos2;
pos1 = 0;
pos2 = str.find(c);
while(string::npos != pos2) {
count++;
pos1 = pos2 + c.size();
pos2 = str.find(c, pos1);
}
if (pos1 != str.length()) {
if(str.substr(pos1) == "CODER")
count++;
}
}
};
/*
思路:分割字符串,统计每个分割后的字符串coder的出现次数
*/
int main(int argc, const char * argv[]) {
string s;
vector<string> vct_str;
vct_str.push_back("iamacoder");
vct_str.push_back("CoderCoder");
vct_str.push_back("COder");
Coder c;
c.findCoder(vct_str, 3);
}
/*
本题的关键问题:
1、 transform(str.begin(), str.end(),str.begin() , ::toupper);
2、不误解题目意思。
*/