P7758 [COCI 2012/2013 #3] HERKABE

分析

容易发现,若(任意)前缀相同,则此类字符串是绑定在一起的(即不可以分开)。

容易想到不同类之间可以交换产生贡献,而同一类中的不同类同样可以产生贡献。

这启发我们,每次找到前缀分类后,继续找当前前缀下一位进行分类,以计算答案。

可以用递归分治实现,每一类的答案就是当前一类以下一位分类的类数的全排列。

总的答案就是所有子答案的阶乘。

码量不大。

Tips:先按字典序排序。

代码

#include<bits/stdc++.h>
#define int long long
using namespace std;

const int MAXN = 3e3 + 7;
const int MOD = 1e9 + 7;

int n, ans, sum[MAXN], cnt;

string s[MAXN];

void solve(int l, int r, int c)
{
    if(l >= r)
        return ;
    int tot = 0;
    for(int i = l; i <= r; i++)
    {
        int nxt = i;
        while(nxt < r and s[nxt + 1][c] == s[i][c])
            ++nxt;
        ++tot;
        (ans *= tot) %= MOD;
        solve(i, nxt, c + 1);
        i = nxt;
    }
    return ;
}

signed main()
{
    scanf("%lld", &n);

    for(int i = 1; i <= n; i++)
    {
        s[i] = "";

        char ch = getchar();
        
        while(ch < 'A' or ch > 'Z')
            ch = getchar();

        while(ch >= 'A' and ch <= 'Z')
        {
            s[i] += ch;
            ch = getchar();
        }
    }

    ans = 1;

    sort(s + 1, s + n + 1);

    solve(1, n, 0);

    printf("%lld", ans);

    return 0;
}

谢谢观看!

posted @ 2026-01-04 11:29  HHMing  阅读(1)  评论(0)    收藏  举报