美文网首页传统数据结构和算法tc
topcoder srm 144 div1 BinaryCode

topcoder srm 144 div1 BinaryCode

作者: mtl6906 | 来源:发表于2018-01-25 23:02 被阅读0次

    这道题作为300分的水题,直接暴力就可以过了,题意是给你一个01串s,可以通过t[i] =s[i-1] + s[i]+ s[i+1],得到一个新串t,

    其中t[0] = s[0] + s[1],t[n-1] = t[n-1]+t[n-2]。我们现在输入的是串t,要求出串s。

    下面贴代码:

    
    #include <iostream>
    #include <vector>
    #include <string>
    #include <cstdlib>
    using namespace std;
    
    class BinaryCode
    {
            private:
                    vector<string> data[50];
            public:
                    vector<string> decode(string val)
                    {
                            memset(data,0,sizeof(data));
                            inits(val);
                            string res;
                            vector<string> buf;
                            int k;
                            for(int i=0;i<data[0].size();i++)
                            {
                                    res = data[0][i];
                                    for(int j=1;j<val.length();j++)
                                    {
                                            for(k=0;k<data[j].size();k++)
                                                    if(cmp(res,data[j][k]))
                                                    {
                                                            res.insert(res.length(),1,data[j][k][2]);
                                                            break;
                                                    }
                                            if(k == data[j].size())
                                                    break;
                                    }
    //                              cout << res << endl;
                                    if(res.length() == val.length()+2)
                                            buf.push_back(res);
                            }
                            if(buf.size() == 0)
                                    buf.push_back("NONE");
                            if(buf.size() == 1)
                                    buf.push_back("NONE");
                            if(buf[0][1] != '1' && buf[1][1] != '0')
                            {
                                    if(buf[0][0] != 'N')
                                            buf[0] = buf[0].substr(1,val.length());
                                    if(buf[1][0] != 'N')
                                            buf[1] = buf[1].substr(1,val.length());
                                    return buf;
                            }
                            else
                            {
                                    string temp = buf[0]; 
                                    buf[0] = buf[1]; 
                                    buf[1] = temp; 
                            }
                            if(buf[0][0] != 'N')
                                    buf[0] = buf[0].substr(1,val.length());
                            if(buf[1][0] != 'N')
                                    buf[1] = buf[1].substr(1,val.length());
                            return buf;
                    }
                    void inits(string val)
                    {
                            for(int i=0;i<val.length();i++)
                                    if(val[i] == '0')
                                            this->data[i].push_back("000");
                                    else if(val[i] == '1')
                                    {
                                            if(i!=val.length()-1)
                                                    data[i].push_back("001");
                                            data[i].push_back("010");
                                            if(i!=0)
                                                    data[i].push_back("100");
                                    }
                                    else if(val[i] == '2')
                                    {
                                            if(i!=0)
                                                    data[i].push_back("110");
                                            if(i!=val.length()-1 && i != 0)
                                                    data[i].push_back("101");
                                            if(i!=val.length()-1)
                                                    data[i].push_back("011");
                                    }
                                    else if(i!=val.length()-1 && i!=0)
                                            data[i].push_back("111");
                    }
                    bool cmp(string s1,string s2)
                    {
                            if(s1[s1.length()-2] == s2[0] && s1[s1.length()-1] == s2[1])
                                    return true;
                            return false;
                    }
    };
     /*
    int main()
    {
            BinaryCode a;
            string s;
            cin >> s;
            vector<string> t = a.decode(s);
            cout << t[0] << " " << t[1] << endl;
            return 0;
    }
    */
    
    
    

    相关文章

      网友评论

        本文标题:topcoder srm 144 div1 BinaryCode

        本文链接:https://www.haomeiwen.com/subject/dowbaxtx.html