比如:AAABCCDDDDEEF
变成:A#3BCCD#4EEF
---------
挺简单的。。
- string encode(string &input){
- string output;
- if(input.empty()) return output;
- int count = 1;
- int i;
- for(i = 0; i < input.length() - 1; i++){
- if(input[i] != input[i+1]){
- if(count < 3){
- while(count-- > 0)
- output.push_back(input[i]);
- }
- else{
- output.push_back(input[i]);
- output.push_back('#');
- output.push_back(to_string(count));
- }
- count = 1;
- }
- else count++;
- }
- output.push_back(input[i]);
- if(count > 1){
- output.push_back('#');
- output.push_back(to_string(count));
- }
- if(count == 2) output.push_back(input[i]);
- return output;
- }
No comments:
Post a Comment