从 X 星截获一份电码,是一些数字,如下:
13
1113
3113
132113
1113122113
....
Y 博士经彻夜研究,发现了规律:
第一行的数字随便是什么,以后每一行都是对上一行“读出来”。
比如第 2 行,是对第 1 行的描述,意思是:1 个 1,1 个 3,所以是:1113。
第 3行,意思是:3 个 1,1 个 3,所以是:3113。
请你编写一个程序,可以从初始数字开始,连续进行这样的变换。
输入格式
第一行输入一个数字组成的串,不超过 100 位。
第二行,一个数字 n,表示需要你连续变换多少次。
输出格式
输出一个串,表示最后一次变换完的结果。
数据范围
1≤n≤20
输入样例:
5
7
输出样例:
13211321322115
题解:
输入的是字符串,使用两个指针,初始都指向0位置,然后当x和y位置的值相同就y++,当不同的时候,y-x的值就是重复字符的个数,更新x,重复n次即可得到结果。
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<queue>
#include<stack>
#include<vector>
#include<unordered_set>
#include<unordered_map>
#include<map>
#include<set>
using namespace std;
typedef long long int ll;
const int INF=1e16;
string a;
int n;
string change(string a){
a+=' ';
string ans;
int x=0,y=0;
while(y<a.size()){
if(a[y]==a[x]){
y++;
}
else{
ans+=(char)(y-x+48);
ans+=a[x];
x=y;
}
}
return ans;
}
void solve(){
cin >> a >> n;
while(n>0){
a=change(a);
n--;
}
cout << a;
}
int main(){
solve();
}