• [leetcode]count and say


    #include <iostream>
    #include <vector>
    #include <stack>
    #include <queue>
    using namespace std;
    
    class Solution {
    public:
        string countAndSay(int n) {//调用process N次
            if (n<1)
                return "";
    
            string str = "1";
            if (n == 1)
                return str;
            for (int i = 1; i < n; i++){
                string str1 = process(str);
                str.assign(str1);
            }
            return str;
        }
    
        string process(const string &str){//每次对于一个str,经过处理后,返回它的count and say
            string result = "";
    
            int left = 0;
            int right = 0;
            while (right < str.length()){
                if (str[right] == str[left]){
                    right++;
                    continue;
                }
    
                int count = right - left;
                char tmp[10];
                sprintf(tmp, "%d%d", count, str[left] - '0');
                string tmp1(tmp);
                result.append(tmp1);
    
                left = right;
            }
            if (str[right-1] == str[left]){
                int count = right - left;
                char tmp[10];
                sprintf(tmp, "%d%d", count, str[left] - '0');
                string tmp1(tmp);
                result.append(tmp1);
            }
            
            return result;
        }
    };
    
    
    int main()
    {
        int n = 5;
        Solution s;
        string result = s.countAndSay(n);
        return 0;
    }

    EOF

  • 相关阅读:
    Codeforces 716C[数论][构造]
    HDU 5808[数位dp]
    Codeforces 611d [DP][字符串]
    Codeforces 404D [DP]
    HDU 5834 [树形dp]
    HDU 5521 [图论][最短路][建图灵感]
    矩阵
    kruskal 处理最短路 问题 A: 还是畅通工程
    Dijastra最短路 + 堆优化 模板
    CodeForces
  • 原文地址:https://www.cnblogs.com/lihaozy/p/2824304.html
Copyright © 2020-2023  润新知