Huawei_test_1.jpg

    • 字符串筛选
    • 字符串截取
    • 字符串计数
    • 去重排序

      1. #include <iostream>
      2. #include <vector>
      3. #include <string>
      4. #include <algorithm>
      5. using namespace std;
      6. int main() {
      7. string Input_str;
      8. while (cin >> Input_str)
      9. {
      10. vector<string> name_vec;
      11. vector<string> name_list;
      12. string temp;
      13. int num[100] = { 0 };
      14. int res_error = 0;
      15. for (int i = 0; i < Input_str.size(); i++) {
      16. //find the illegal sign if happens change the flag
      17. if ((Input_str[i]<'A' || (Input_str[i] > 'Z'&&Input_str[i] < 'a') || Input_str[i]>'z') && Input_str[i] != ',') {
      18. res_error = 1;
      19. break;
      20. }
      21. //Use comma as flag to split the string
      22. if (Input_str[i] != ',')
      23. temp += Input_str[i];
      24. else {
      25. name_vec.push_back(temp);
      26. temp.clear();
      27. }
      28. }
      29. //deal with the last one without comma
      30. name_vec.push_back(temp);
      31. //sort the list of name
      32. name_list = name_vec;
      33. sort(name_list.begin(), name_list.end());
      34. name_list.erase(unique(name_list.begin(), name_list.end()), name_list.end());
      35. //calculate the number of the name
      36. for (int i = 0; i < name_vec.size(); i++)
      37. for (int j = 0; j < name_list.size(); j++)
      38. if (name_vec[i] == name_list[j]) {
      39. num[j]++;
      40. break;
      41. }
      42. //fine the most one
      43. int max = 0;
      44. for (int i = 0; i < 99; i++)
      45. if (num[i] > max)
      46. max = num[i]; //there is an error in the test
      47. //pick the most ticket' owner
      48. vector<string> equal_name;
      49. for (int i = 0; i < name_list.size(); i++)
      50. if (num[i] == max)
      51. equal_name.push_back(name_list[i]);
      52. //sort again
      53. sort(equal_name.begin(), equal_name.end());
      54. if (res_error == 1) {
      55. cout << "error.0001" << endl;
      56. continue;
      57. }
      58. cout << equal_name[0] << endl;
      59. }
      60. }