forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountAndSay.cpp
More file actions
63 lines (57 loc) · 1.26 KB
/
countAndSay.cpp
File metadata and controls
63 lines (57 loc) · 1.26 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
61
62
63
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
typedef long long LL;
int s2i(string s) {
stringstream ss;
ss << s;
int res;
ss >> res;
return res;
}
string i2s(int n) {
stringstream ss;
ss << n;
string res;
ss >> res;
return res;
}
class Solution {
public:
string countAndSay(int n) {
string s = "1";
REP(i,n-1) {
s += "0";
string t = "";
int len = s.length();
char last = '0';
int cnt = 0;
REP(j,len) {
if (s[j] != last) {
if (cnt != 0) {
t += i2s(cnt);
t += last;
}
last = s[j];
cnt = 1;
} else {
++cnt;
}
}
s = t;
}
return s;
}
};
int main() {
Solution s;
FOR(i,1,5) {
cout << s.countAndSay(i) << endl;
}
return 0;
}