UVA

来源:互联网 发布:人肉俄罗斯转盘 知乎 编辑:程序博客网 时间:2024/06/06 03:40

题目链接:https://vjudge.net/problem/UVA-1262

题目大意:5列中每列取出一个字母,这个字母在两张表中的同一列都出现过,求组成的字符串第K大的字符串。

解题思路:暴力枚举,坑点就是每一列的字母可能重复

AC代码:

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;char gt[2][6][5], ch[5][6], ansch[5];int chtot[5], ranking, k;bool vis[5][300];void Dfs(int t){    if (t == 5)    {        ranking++;        if (ranking == k)        {            for (int i = 0;i < 5;i++)                printf("%c", ansch[i]);            puts("");        }        return;    }    for (int i = 0;i < chtot[t];i++)    {        if (ranking != k)        {            ansch[t] = ch[t][i];            Dfs(t + 1);        }    }}int main(){    int t;scanf("%d", &t);    while (t--)    {        scanf("%d", &k);        for (int i = 0;i < 2;i++)            for (int j = 0;j < 6;j++)                for (int l = 0;l < 5;l++)                    scanf(" %c", &gt[i][j][l]);        memset(vis, 0, sizeof(vis));        memset(chtot, 0, sizeof(chtot));        ranking = 0;        for (int i = 0;i < 6;i++)            for (int j = 0;j < 5;j++)                vis[j][gt[0][i][j] - 'A'] = 1;        for (int i = 0;i < 6;i++)            for (int j = 0;j < 5;j++)                if (vis[j][gt[1][i][j] - 'A'])                    ch[j][chtot[j]++] = gt[1][i][j];        for (int i = 0;i < 5;i++)        {            sort(ch[i], ch[i] + chtot[i]);            chtot[i] = unique(ch[i], ch[i] + chtot[i]) - ch[i];        }        int ans = 1;        for (int i = 0;i < 5;i++)            ans *= chtot[i];        if (ans < k)            puts("NO");        else            Dfs(0);    }    return 0;}