PTA乙级(1078 字符串压缩与解压 (20分))
1078 字符串压缩与解压 (20分)
https://pintia.cn/problem-sets/994805260223102976/problems/994805262018265088
1 #include <cstdio> 2 #include <cstring> 3 #include <string> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 int main() 8 { 9 string a,b,num; 10 char chr; 11 cin>>chr; 12 getchar(); 13 getline(cin,a); 14 int len=a.length(); 15 int cnt=1; 16 if(chr=='D') 17 { 18 for(int i=0;i<len;i++) 19 { 20 if(a[i]>='0'&&a[i]<='9') num+=a[i]; 21 else { 22 if(num.length()>0) cnt=stoi(num); 23 while(cnt--) cout<<a[i]; 24 cnt=1; 25 num=""; 26 } 27 } 28 }else if(a.length()>0) 29 { 30 char pre=a[0]; 31 for(int i=1;i<len;i++) 32 { 33 if(a[i]==pre) cnt++; 34 else { 35 if(cnt>1) cout<<cnt; 36 cout<<pre; 37 cnt=1; 38 pre=a[i]; 39 } 40 } 41 if(cnt>1) cout<<cnt; 42 cout<<pre<<endl; 43 } 44 return 0; 45 }
天晴了,起飞吧