hdoj1020简单的字符串处理

来源:互联网 发布:农村淘宝报名入口 编辑:程序博客网 时间:2024/04/29 11:32
#include<iostream>#include<string>using namespace std;int main(){string s1;char ch[10005];int m;int count1 = 1;int count2 = 0;cin>>m;for(int i = 0; i < m; i++){//依次处理每一行count2 = 0;cin>>s1;if(s1.length() == 1){cout<<s1<<endl;continue;}for(int j = 1; j < s1.length(); j++){//依次处理每个字符if(s1[j] == s1[j - 1]){count1++;}else {if(count1 > 1){count2++;//ch[count2 - 1] = (char)(count1 + 48);char temp[10];sprintf(temp, "%d", count1);//count1的值可能大于个位数for(int k = 0; temp[k] != '\0'; k++, count2++){ch[count2 - 1] = temp[k];}count2--;}count2++;ch[count2 - 1] = s1[j - 1];count1 = 1;}}if(count1 != 1){count2++;//ch[count2 - 1] = (char)(count1 + 48);char temp[10];sprintf(temp, "%d", count1);for(int k = 0; temp[k] != '\0'; k++, count2++){ch[count2 - 1] = temp[k];}count2--;//很多的count2++与count2--只是为了保持逻辑的直接性count2++;ch[count2 - 1] = s1[s1.length() - 1];count1 = 1;}else{count2++;ch[count2 -1] = s1[s1.length() - 1];}ch[count2] = '\0';cout<<ch<<endl;}}

原创粉丝点击