Home PAT 1140 Look-and-say Sequence
Post
Cancel

PAT 1140 Look-and-say Sequence

Link

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <string.h>
#include <vector>
using namespace std;
int d,n;
int main() {
    scanf("%d%d",&d,&n);
    string s=to_string(d);
    for(int i=2;i<=n;++i){
        int cnt=1,j=1;
        string ss="";
        for(;j<s.size();++j){
            if(s[j]==s[j-1])
                cnt++;
            else{
                ss+=s[j-1]+to_string(cnt);
                cnt=1;
            }
        }
        ss+=s[j-1]+to_string(cnt);
        s=ss;
    }
    cout<<s<<endl;
    return 0;
}
This post is licensed under CC BY 4.0 by the author.