-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountAndSay.cpp
More file actions
60 lines (47 loc) · 1.11 KB
/
CountAndSay.cpp
File metadata and controls
60 lines (47 loc) · 1.11 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<bits/stdc++.h>
using namespace std;
vector<vector<int>> count(string s){
vector<vector<int>> v;
char key=s[0];
int count=0;
int i=0;
while(i<s.length()){
if(key==s[i]){
count++;
i++;
}else{
v.push_back({count,(int)key-48});
key=s[i];
count=0;
}
}
v.push_back({count,(int)key-48});
return v;
}
string itoa(int n){
string s="";
int r=0;
while(n>0){
r=n%10;
s+=(char)(r+48);
n=n/10;
}
reverse(s.begin(), s.end());
return s;
}
string countAndSay(int n) {
if(n==1){
return "1";
}
string s= countAndSay(n-1);
vector<vector<int>> v=count(s);
string str="";
for(int i=0;i<v.size();i++){
str+=itoa(v[i][0])+itoa(v[i][1]);
}
return str;
}
int main(){
cout<<countAndSay(5)<<endl;
return 0;
}