POJ3359 UVA1209 LA3173 Wordfish【Ad Hoc】

Wordfish
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1082 Accepted: 535

Description

You have been tasked to infiltrate a tight-lipped society for fun and profit: the ACM ICPC regional judges. Through the PC2 “submission” software, you know that classified information is accessible through the log-ins of the judges tasked to a particular “regional site”. However, you are not certain that any particular judge has access to all the relevant information, so several log-ins will be required. You have been handed down a list of usernames, and the passwords used can be derived from these usernames, as follows:

Input

The input will only have capital letters (denoting the usernames) and carriage returns. Each line (thus each username) will not be longer than twenty characters, and there will not be more than 12 “judges” whose log-ins you will need to infiltrate. Strangely, no username uses any letter more than once.

Output

For each username, you must produce a line containing the password of length within 20 which that username uses. The password for a given username is determined from the twenty-one lexicographically consecutive permutations of the username, the eleventh (middle) of which is the username itself. For example, if the username is WORDFISH, the lexicographic permutations of WORDFISH contain, in order:

…, WOISHRFD, WOISRDFH, WOISRDHF, WOISRFDH, WOISRFHD, WOISRHDF, WOISRHFD, WORDFHIS, WORDFHSI, WORDFIHS, WORDFISH, WORDFSHI, WORDFSIH, WORDHFIS, WORDHFSI, WORDHIFS, WORDHISF, WORDHSFI, WORDHSIF, WORDIFHS, WORDIFSH, …

The password is then the permutation among the twenty-one lexicographically consecutive permutations of the username which has the largest minimum absolute distance between consecutive letters (and the first amongst the lexicographically ordered, if several permutations have the largest minimum absolute distance), followed by that minimum absolute distance. For the username WORDFISH, the password is WORDHSFI3.

Sample Input

WORDFISH

Sample Output

WORDHSFI3

Source

Manila 2006

Regionals 2006 >> Asia - Manila

問題鏈接POJ3359 UVA1209 LA3173 Wordfish
問題簡述:(略)
問題分析
    雖然是一個簡單題,還是用到了全排列函數,值得了解細節。
程序說明:(略)
參考鏈接:(略)
題記:(略)

AC的C++語言程序如下:

/* POJ3359 UVA1209 LA3173 Wordfish */

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <climits>

using namespace std;

int diff(string& s)
{
    int mind = INT_MAX;
    for(int i = 0; i < (int)s.size() - 1; i++)
        mind = min(mind, abs(s[i + 1] - s[i]));
    return mind;
}

bool cmp(pair<string, int> a, pair<string, int> b)
{
    return a.second == b.second ? a.first > b.first : a.second > b.second;
}

int main()
{
    string usr, usr2;
    while(cin >> usr) {
        vector<pair<string, int> > vp;

        usr2 = usr;
        for(int i = 1; i <= 10; i++) {
            prev_permutation(usr.begin(), usr.end());
            vp.push_back(make_pair(usr, diff(usr)));
        }
        vp.push_back(make_pair(usr2, diff(usr2)));
        for(int i = 1; i <= 10; i++) {
            next_permutation(usr2.begin(), usr2.end());
            vp.push_back(make_pair(usr2, diff(usr2)));
        }

        sort(vp.begin(), vp.end(), cmp);

        int d = vp[0].second;
        for(int i = 0; i < (int)vp.size(); i++) {
            if(vp[i].second < d) {
                printf("%s%d\n", vp[i - 1].first.c_str(), vp[i - 1].second);
                break;
            }
            if(d == 1) {
                printf("%s%d\n", vp[(int)vp.size() - 1].first.c_str(), vp[(int)vp.size() - 1].second);
                break;
            }
        }
    }

    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章