C++笔试题 字符串处理

版权声明:代码属于原创,转载请联系作者并注明出处。 https://blog.csdn.net/weixin_43379056/article/details/83511170

字符串处理

描述

定义字符串的以下几种操作:
• reverse(A)获得A字符串的逆序字符串,例如reverse(“abc”) = “cba”
• shuffle(A)获得A随机重排的字符串,例如shuffle(“dog”) ∈ {“dog”, “dgo”, “odg”, “ogd”, “gdo”, “god”}
• merge(A1, A2),合并A1和A2两个字符串,合并过程中只保证A1和A2本身字母的顺序,例如merge(“qwe”, “asd”)的可能结果有很多, “qweasd”和“qwased”都是可能的取值。现在给定一个字符串S,S ∈merge(reverse(A), shuffle(A))。求以字母表顺序排序的A的最小值。

输入描述

输入一个字符串S。

输出描述

输出一个字符串,为A的最小取值。

样例输入 1

abcdefgabcdefg

样例输出 1

agfedcb

提示

reverse(“agfedcb”) = “bcdefga”
shuffle(“agfedcb”) = “abcdefg”
merge(“bcdefga”, “abcdefg”) = “abcdefgabcdefg”

#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <iterator>
#include <algorithm>  // next_permutation() and sort() 

using namespace std;

// #define UNIT_TEST

class CambrianStringProcessing
{
private:
    int  factorial(const int n);
    bool checkSequence(const string &s, const string &Str);

public:
    string reverse(const string& s);
    vector<string> shuffle(const string& s);
    vector<string> merge(const string& A1, const string& A2);
    bool merge(const string& A1, const string& A2, const string &S);
    string findMinimumStringA(const string &S);
};

string CambrianStringProcessing::reverse(const string& s)
{
    string ts;

    ts.assign(s.rbegin(), s.rend());

    return ts;
}

vector<string> CambrianStringProcessing::shuffle(const string& s)
{
    string ts = s;
    vector<string> shuffleVector;

    for (int i = 1; i <= factorial(s.size()); i++)
    {
        next_permutation(ts.begin(), ts.end());
        shuffleVector.push_back(ts);
    }
    return shuffleVector;
}
    
int CambrianStringProcessing::factorial(const int n)
{
    int f = n;

    for (int i = f - 1; i >= 1; i--)
    {
        f *= i;
    }

    return f;
}

bool CambrianStringProcessing::checkSequence(const string &s, const string &Str)
{
    int pos, prevPos = 0;
    for (int i = 0; i < s.size(); i++)
    {
        pos = Str.find(s[i], prevPos);
        if (pos != string::npos)
        {
            prevPos = pos + 1;
        }
        else
        {
            return false;
        }
    }
    return true;
}

bool CambrianStringProcessing::merge(const string& A1, const string& A2, const string &S)
{
    if (true == checkSequence(A1, S) &&
        true == checkSequence(A2, S))
    {
        return true;
    }
    return false;
}

vector<string> CambrianStringProcessing::merge(const string& A1, const string& A2)
{
    vector<string> mergeVector;
    string newStr = A1 + A2;

    for (int i = 1; i <= factorial(newStr.size()); i++)
    {
        next_permutation(newStr.begin(), newStr.end());

        if (true == checkSequence(A1, newStr) &&
            true == checkSequence(A2, newStr))
        {
            mergeVector.push_back(newStr);
        }
    }
    return mergeVector;
}

string CambrianStringProcessing::findMinimumStringA(const string &S)
{
    set<char> chS;
    for (int i = 0; i < S.size(); i++)
    {
        chS.insert(S[i]);
    }

    string A;
    A.assign(chS.begin(), chS.end());

    string Ar;             // reverse(A)
    vector<string> strVs;  // save all results of shuffle(A)
    vector<string> strAs;  // save all A string  
    int size = A.size();   // the length of string A

    for (int i = 1; i <= factorial(size); i++)
    {
        next_permutation(A.begin(), A.end());

        Ar = reverse(A);

        strVs = shuffle(A);

        bool found = false;
        for (int i = 0; false == found && i < strVs.size(); i++)
        {
            if (true == merge(Ar, strVs[i], S))
            {
#ifdef UNIT_TEST
                cout << "reverse(" << A << ") = " << Ar << endl;
                cout << "shuffle(" << A << ") = " << strVs[i] << endl;
                cout << "merge(" << Ar << ", ";
                cout << strVs[i] << ") = " << S << endl << endl;
#endif
                return A;
            }
        }
    }

    return A;
}

int main()
{
    CambrianStringProcessing cms;

#ifdef UNIT_TEST    
    string A("abc");
    cout << cms.reverse(A) << endl;

    A = "dog";
    vector<string> strVs = cms.shuffle(A);

    for (unsigned int i = 0; i < strVs.size(); i++)
    {
        cout << strVs[i] << endl;
    }

    string A1 = "qwe";
    string A2 = "asd";

    vector<string> strVm = cms.merge(A1, A2);

    for (unsigned int i = 0; i < strVm.size(); i++)
    {
        cout << strVm[i] << endl;
    }
#endif

    string S;

    cin >> S;

    cout << cms.findMinimumStringA(S) << endl;

    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43379056/article/details/83511170