1 class Solution {
2 public:
3 string countAndSay(int n) {
4 list<int>v;
5 v.push_back(1);
6 while (--n)
7 {
8 list<int>tmp;
9
10 int num = -1;
11 int count = 0;
12 while (!v.empty())
13 {
14 if (num == -1) {
15 num = v.front();
16 count++;
17 }
18 else if (v.front() == num)
19 {
20 count++;
21 }
22 else
23 {
24 tmp.push_back(count);
25 tmp.push_back(num);
26 count = 0;
27 num = v.front();
28 count++;
29 }
30 v.pop_front();
31 }
32
33 tmp.push_back(count);
34 tmp.push_back(num);
35
36 v = tmp;
37 }
38
39 string str = "1";
40
41 if (!v.empty())
42 str = "";
43
44 while (!v.empty()){
45 str += v.front() + '0';
46 v.pop_front();
47 }
48
49 return str;
50 }
51 };
52