示例1:
1,2<A>00
示例2:
1,2<A>00,3<A>00
示例3:
<B>12,1,2<B>1
示例4:
<B<12,1
输出依次如下:
#include<iostream>
#include<vector>
#include<string>using namespace std;/*
字符分割函数 将传入的字符串按照字符分割 放入vector中
*/
vector<string> split_str(string str_input, char c)
{vector<string> result;while(str_input.find(c) != string::npos){int pos_index = str_input.find(c);cout<<"pos_index:"<<pos_index<<endl;string str_tmp = str_input.substr(0, pos_index);//cout<<"str_tmp:"<<str_tmp<<endl;//int digital = stoi(str_tmp);result.push_back(str_tmp);str_input = str_input.substr(pos_index+1);//cout<<"str_input:"<<str_input<<endl;}//int digital = stoi(str_input);cout<<"str_input:"<<str_input<<endl;result.push_back(str_input);return result;
}int main() {string input_str;getline(cin, input_str);vector<string> str_vec;str_vec = split_str(input_str, ',');int count = 0;count = str_vec.size();for (int index = 0; index < count; index++) {string cur_str = str_vec[index];int result1 = cur_str.find("<");int result2 = cur_str.find(">");cout <<"result1:"<<result1<<" result2:"<<result2<<""<<endl;if (result1 == -1 && result2 == -1) {////cout << "case 1"<<endl;} else if(result1>-1 && result2>-1){//cout << "case 2"<<endl;char target_pos = cur_str.substr(result1 + 1, result2)[0];char cur_pos = index + 'A';if (!(target_pos >= 'A' && target_pos <= 'Z')) {//}if (cur_pos == target_pos) {//}int target_index = target_pos - 'A';string temp_result = "";temp_result += cur_str.substr(0, result1);temp_result += str_vec[target_index];temp_result += cur_str.substr(result2 + 1);str_vec[index] = temp_result;}else{//cout << "case 3"<<endl;cout << "-1"<<endl;exit(0);}}count = str_vec.size();for (int i = 0; i < count; i++) {cout << str_vec[i];if (i != count - 1) {cout << ",";}}cout<<""<<endl;return 0;
}