fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <sstream>
  5.  
  6. typedef std::pair<char, std::size_t> CS;
  7. typedef std::vector<CS> VecCS;
  8.  
  9. std::string StringCast(int N){
  10. std::stringstream ss;
  11.  
  12. ss << N;
  13. return ss.str();
  14. }
  15.  
  16. VecCS CountChar(std::string str){
  17. CS P = std::make_pair(str[0], 0);
  18. VecCS vec;
  19.  
  20. for (std::size_t i = 0; i < str.size(); i++){
  21. if (P.first != str[i]){
  22. vec.push_back(P);
  23. P = std::make_pair(str[i], 1);
  24. }
  25. else{
  26. P.second++;
  27. }
  28. }
  29. vec.push_back(P);
  30. return vec;
  31. }
  32.  
  33. std::string ToString(VecCS vec){
  34. std::string str;
  35. for (auto& o: vec){
  36. str += StringCast(o.second);
  37. str += o.first;
  38. }
  39.  
  40. return str;
  41. }
  42.  
  43. int main(){
  44. std::string str = "1";
  45. std::size_t N = 10;
  46. std::size_t i=0;
  47. char *Answer[] = { "1", "11", "21", "1211", "111221", "312211", "13112221", "1113213211", "31131211131221", "13211311123113112211", "11131221133112132113212221" };
  48.  
  49.  
  50. for (i = 0; i < N; i++){
  51. std::cout << i << ":" << str << ' ' << ((str == Answer[i]) ? " " : "Invalid!!") << std::endl;
  52. auto ret = CountChar(str);
  53. str = ToString(ret);
  54. }
  55. std::cout << i << ":" << str << ' ' << ((str == Answer[i]) ? " " : "Invalid!!") << std::endl;
  56.  
  57. return 0;
  58. }
Success #stdin #stdout 0s 3480KB
stdin
Standard input is empty
stdout
0:1  
1:11  
2:21  
3:1211  
4:111221  
5:312211  
6:13112221  
7:1113213211  
8:31131211131221  
9:13211311123113112211  
10:11131221133112132113212221