1015.cpp

来自「杭电 acm部分代码 有兴趣的可以下载 谢谢」· C++ 代码 · 共 56 行

CPP
56
字号
#include<iostream>
#include<algorithm>
#include<functional>
#include<string>
#include<list>
using namespace std;
int f[10][30];
int num;
string str;
bool unequal(char a, char b, char c, char d, char e)
{
    if(a - b && b - c && c - d && d - e && a - c && a - d && a - e && b - d && c - e && b - e)
        return true;
    else
        return false;
}
int main()
{
    int a, b;
    for(a = 0; a < 26; a++)
        f[1][a] = a + 1;
    for(a = 2; a < 6; a++)
        for(b = 0; b < 26; b++)
            f[a][b] = f[a - 1][b] * f[1][b];
    while(cin >> num >> str)
    {
        if(num == 0 && str == "END")
            break;
        int len = str.length();
        list<string> V;
        string :: iterator i, j, k, l, m;
        for(i = str.begin(); i != str.end(); i++)
            for(j = str.begin(); j != str.end(); j++)
                for(k = str.begin(); k != str.end(); k++)
                    for(l = str.begin(); l != str.end(); l++)
                        for(m = str.begin(); m != str.end(); m++)
                            if(unequal(*i, *j, *k, *l, *m) && f[1][*i - 'A'] - f[2][*j - 'A'] + f[3][*k - 'A'] - f[4][*l - 'A'] + f[5][*m - 'A'] == num)
                            {
                                string temp;
                                temp+=*i;
                                temp+=*j;
                                temp+=*k;
                                temp+=*l;
                                temp+=*m;
                                V.push_back(temp);
                            }
        if(V.empty())
            cout << "no solution" << endl;
        else
        {
            V.sort(greater<string>());
            cout << V.begin()->data() << endl;
        }
    }
    return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?