1078 字符串压缩与解压 (20 分)

#include <bits/stdc++.h> 
using namespace std;
int main(){
    int i,j,cnt=1;
    char n;
    string s,num;
    cin>>n;
    getchar();
    getline(cin,s);
    if(n=='D'){
        for(i=0;i<s.length();i++){
            if(s[i]>='0'&&s[i]<='9'){
                num+=s[i];
            }
            else{
                if(num.length()>0){
                    cnt=stoi(num);
                }
                while(cnt--){
                    cout<<s[i];
                }
                cnt=1;
                num="";
            }
        }
    }
    else{
        char pre=s[0];
        for(i=1;i<s.length();i++){
            if(s[i]==pre){
                cnt++;
            }
            else{
                if(cnt>=2)
                    cout<<cnt;
                cout<<pre;
                cnt=1;
                pre=s[i];
            }
        }
        if(cnt>=2) cout<<cnt;
        cout<<pre;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/tonyyy/p/10485513.html