信息学奥赛一本通 1145. 字符串p型编码
原题链接
中等
作者:
lkjy
,
2025-05-10 10:51:52
· 山东
,
所有人可见
,
阅读 3
题目描述
给定一个完全由数字字符(‘0’,‘1’,‘2’,…,‘9’)构成的字符串str,请写出str的p型编码串。
例如:字符串122344111可被描述为"1个1、2个2、1个3、2个4、3个1",
因此我们说122344111的p型编码串为1122132431;
类似的道理,编码串101可以用来描述1111111111;
00000000000可描述为"11个0",因此它的p型编码串即为110;
100200300可描述为"1个1、2个 0、1个2、2个0、1个3、2个0",
因此它的p型编码串为112012201320。
样例
【输入样例】
122344111
【输出样例】
1122132431
算法
C++ 代码
#include<string>
#include<iostream>
using namespace std;
int main(){
string str;
cin>>str;
int len = str.length();
int count = 1;
for(int i = 0;i<len;i++){
if(str[i+1] == str[i]){
count++;
continue;
}
cout<<count<<str[i];
count = 1;
}
return 0;
}