zoukankan      html  css  js  c++  java
  • Count and Say

    The count-and-say sequence is the sequence of integers beginning as follows:
    1, 11, 21, 1211, 111221, ...

    1 is read off as "one 1" or 11.
    11 is read off as "two 1s" or 21.
    21 is read off as "one 2, then one 1" or 1211.

    Given an integer n, generate the nth sequence.

    Note: The sequence of integers will be represented as a string.

    数字转换为string,使用stringstream流,字符直接使用append追加到string后面。注意,对于流,每次用完要清空。

    C++代码实现:

    #include<iostream>
    #include<string>
    #include<sstream>
    using namespace std;
    
    class Solution
    {
    public:
        string countAndSay(int n)
        {
            if(n==0)
                return NULL;
            if(n==1)
                return string("1");
            string s="1";
            int i;
            size_t j,k=0;
            for(i=2; i<=n; i++)
            {
                string tmp;
                stringstream ss;
                for(j=1; j<s.length(); j++)
                {
                    if(s[k]!=s[j])
                    {
                        ss<<j-k;
                        tmp+=ss.str();
                        //注意清空stringstream,不然上次的内容还存在,还有清空操作不是clear()
                        ss.str("");
                        tmp.append(1,s[k]);
                        k=j;
                    }
                }
                ss<<j-k;
                tmp+=ss.str();
                ss.str("");
                tmp.append(1,s[k]);
                s=tmp;
                k=0;
            }
            return s;
        }
    };
    int main()
    {
        Solution s;
        for(int i=1; i<=10; i++)
            cout<<s.countAndSay(i)<<endl;
    }

    运行结果:

     改进版
    #include<iostream>
    #include<string>
    #include<sstream>
    using namespace std;
    
    class Solution
    {
    public:
        string countAndSay(int n)
        {
            if(n==0)
                return NULL;
            if(n==1)
                return string("1");
            string s="1";
            int i;
            size_t j,k=0;
            for(i=2; i<=n; i++)
            {
                string tmp(s);
                j=0;
                int m=tmp.size();
                s.clear();
                while(j<m)
                {
                    k=j+1;
                    while(k<m&&tmp[j]==tmp[k]) k++;
                    int len=k-j;
                    s+=to_string(len);
                    s.append(1,tmp[j]);
                    j=k;
                }
            }
            return s;
        }
    };
    int main()
    {
        Solution s;
        for(int i=1; i<=10; i++)
            cout<<s.countAndSay(i)<<endl;
    }
  • 相关阅读:
    hdu 5446 Unknown Treasure lucas和CRT
    Hdu 5444 Elven Postman dfs
    hdu 5443 The Water Problem 线段树
    hdu 5442 Favorite Donut 后缀数组
    hdu 5441 Travel 离线带权并查集
    hdu 5438 Ponds 拓扑排序
    hdu 5437 Alisha’s Party 优先队列
    HDU 5433 Xiao Ming climbing dp
    hdu 5432 Pyramid Split 二分
    Codeforces Round #319 (Div. 1) B. Invariance of Tree 构造
  • 原文地址:https://www.cnblogs.com/wuchanming/p/4105055.html
Copyright © 2011-2022 走看看