#include<iostream> #include<string> using namespace std; void string_zip(string input,string &output) { if(input.empty()) return ; char last='\0'; char current; int count=0; for(int i=0;i<input.size();++i) { current=input[i]; if(current==last||last=='\0') { count++; last=current; } else { if(count>1) { output.push_back(count+'0'); } output.push_back(last); count=1; last=current; } } if(count>1) output.push_back(count+'0'); output.push_back(last); } int main() { string input("aabbbbccd"); string output; string_zip(input,output); cout<<output<<endl; system("pause"); return 0; }
时间: 2024-10-06 06:18:07