2019 ICPC上海网络赛-F. Rhyme scheme

F.Rhyme scheme

 Rhyme scheme

问答问题反馈

编辑代码

  •  31.21%
  •  1000ms
  •  65536K

A rhyme scheme is the pattern of rhymes at the end of each line of a poem or song. It is usually referred to by using letters to indicate which lines rhyme; lines designated with the same letter all rhyme with each other.

e.g., the following "poem'' of 44 lines has an associated rhyme scheme "ABBA''

1 —— 9999 bugs in the code A

2 —— Fix one line B

3 —— Should be fine B

4 —— 100100 bugs in the code A

This essentially means that line 11 and 44 rhyme together and line 22 and 33 rhyme together.

The number of different possible rhyme schemes for an nn-line poem is given by the Bell numbers. For example, B_3 = 5B3​=5, it means there are five rhyme schemes for a three-line poem: AAA, AAB, ABA, ABB, and ABC.

The question is to output the kk-th rhyme scheme in alphabetical order for a poem of nn lines.For example: the first rhyme scheme of a three-line poem is "AAA'', the fourth rhyme scheme of a three-line poem is ABB''.

InputFile

The first line of the input gives the number of test cases, TT (1 \leq T \leq 100001≤T≤10000). TT test cases follow.

Each test case contains a line with two integers nn and kk.

1 \leq n \leq 26, 1 \leq k \leq B_n1≤n≤26,1≤k≤Bn​ (B_nBn​ is the nn-th of Bell numbers)

OutputFile

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 11) and yy is the rhyme scheme contains uppercase letters.

样例输入复制

7
1 1
2 1
3 1
3 2
3 3
3 4
3 5

样例输出复制

Case #1: A
Case #2: AA
Case #3: AAA
Case #4: AAB
Case #5: ABA
Case #6: ABB
Case #7: ABC

 思路:

我们可以画一颗这样的树先。

2019 ICPC上海网络赛-F. Rhyme scheme_第1张图片

观察这一颗树,可以发现,每个结点如果按字典序安排子结点,那么叶子节点一定是从1~Bn的顺序。

那么,随意观察一条路径,一个结点可以出现字母x当且仅当其祖先结点出现了字母x-1,也就是说,由第一层到当前层的出现字母的最大值为x或x-1,才可以在当前结点放x。

如果我们将dp[i][j]表示成在第i层,已出现的最大字母为'A'+j-1,那么, 有状态转移方程dp[i][j] = dp[i+1][j]*j + dp[i+1][j+1].

这个转移方程的含义是,在第i层已出现的最大字母为'A'+j-1的状态,是由在第i+1层,已出现的最大字母为j或j+1转移而来的,如果当前层最大值为j,下一层状态最大值仍然为j时,那么下一层的那个结点就是填1~j的情况,才能保证最大值仍然为j,;如果当前层最大值为j,下一层最大值为j+1,那么下一层只能填j+1这一种字母,才能改变状态。

这样,我们就可以由尾到头地预先处理出所有状态的种类,而后dfs剪枝填字母解决问题。

注意,由于B26是一个巨大的数字,会爆long long,题解采用的写法是将两个long long拼在一起使用,ORZ。

无耻地贴了一发正解。

#include 
using namespace std;

const long long MOD = 1e12;
const int DLEN = 12;

struct Num 
{
    long long a, b;
    Num() 
    {
        a = b = 0;
    }
Num(int v) 
{
    a = 0;
    b = v;
}
Num(long long v) 
{
    a = v/MOD;
    b = v%MOD;
}
    Num(const char s[]) 
    {
        a = b = 0;
        int L = strlen(s);
        for (int i = 0; i < L; i++) 
        {
            b = b*10 + s[i] - '0';
            a = a*10;
            a += b/MOD;
            b %= MOD;
        }
    }
    void output() 
    {
        cout<= 1; i--) 
        {
            if (i == n) 
            {
                for (int j = 1; j <= i; j++)
                    dp[n][i][j].b = 1LL;
            } 
            else 
            {
                for (int j = 1; j <= i; j++) 
                    dp[n][i][j] = dp[n][i+1][j] * j + dp[n][i+1][j+1];
            }
        }
    }
        int T;
        int iCase = 0;
        scanf("%d", &T);
        while (T--) 
        {
            iCase++;
            int n;
            scanf("%d", &n);
            scanf("%s", str);
            Num k = Num(str);
            printf("Case #%d: A", iCase);
            dfs(n, k, 1, 1);
            puts("");
        }
    return 0;
}

 

你可能感兴趣的:(算法学习,基础算法,动态规划)