【bzoj3012】[Usaco2012 Dec]First!

本文介绍了一道关于寻找字典序最小字符串的算法题目,通过构建Trie树并运用图论中的拓扑排序思想来解决。文章详细解析了解题思路及实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

Bessie has been playing with strings again. She found that by changing the order of the alphabet she could make some strings come before all the others lexicographically (dictionary ordering). For instance Bessie found that for the strings “omm”, “moo”, “mom”, and “ommnom” she could make “mom” appear first using the standard alphabet and that she could make “omm” appear first using the alphabet “abcdefghijklonmpqrstuvwxyz”. However, Bessie couldn’t figure out any way to make “moo” or “ommnom” appear first. Help Bessie by computing which strings in the input could be lexicographically first by rearranging the order of the alphabet. To compute if string X is lexicographically before string Y find the index of the first character in which they differ, j. If no such index exists then X is lexicographically before Y if X is shorter than Y. Otherwise X is lexicographically before Y if X[j] occurs earlier in the alphabet than Y[j].

给定n个总长不超过m的互不相同的字符串,现在你可以任意指定字符之间的大小关系。问有多少个串可能成为字典
序最小的串,并输出这些串。n <= 30,000 , m <= 300,000
Input

  • Line 1: A single line containing N (1 <= N <= 30,000), the number of strings Bessie is playing with.

  • Lines 2..1+N: Each line contains a non-empty string. The total number of characters in all strings will be no more than 300,000. All characters in input will be lowercase characters ‘a’ through ‘z’. Input will contain no duplicate strings.

Output

  • Line 1: A single line containing K, the number of strings that could be lexicographically first.

  • Lines 2..1+K: The (1+i)th line should contain the ith string that could be lexicographically first. Strings should be output in the same order they were given in the input.

Sample Input

4

omm

moo

mom

ommnom

INPUT DETAILS: The example from the problem statement.

Sample Output

2

omm

mom

OUTPUT DETAILS: Only “omm” and “mom” can be ordered first.

题解
把字符串加入trie树中,如果一个字符串字典序最小,那么同一位置上的字符要比其它字符优先级高,由高的向底的连边,判断是否会出现环。

代码

#include<bits/stdc++.h>
typedef long long ll;
const int N=10000000;
const int mod=1000000007;
const double eps=0.00000001;
using namespace std;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9'){if (ch=='-')f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n,ans[30005],cnt,tot;
int t[300005][26],du[27],a[27][27];
char s[30003][305];
bool mark[300005];
inline void insert(char s[305])
{
    int now=0,n=strlen(s+1);
    for (int i=1;i<=n;i++)
    {
        int x=s[i]-'a';
        if (!t[now][x]) t[now][x]=++tot;
        now=t[now][x];
    }
    mark[now]=1;
}
inline void clear()
{
    for (int i=0;i<26;i++)
    {
        du[i]=0;
        for (int j=0;j<26;j++)
            a[i][j]=0;
    }

}
inline void query(char s[305])
{
    int now=0,n=strlen(s+1);
    for (int i=1;i<=n;i++)
    {
        int x=s[i]-'a';if (mark[now]) a[x][x]=1,du[x]++;
        for (int j=0;j<26;j++) if (t[now][j]&&j!=x) a[x][j]++,du[j]++;
        now=t[now][x];
    }
}
bool judge()
{
    queue<int>q;int sum=26;
    for (int i=0;i<26;i++) if (!du[i]) q.push(i);
    while (!q.empty())
    {
        int now=q.front();q.pop();sum--;
        for (int i=0;i<26;i++)if (a[now][i])
        {
            du[i]-=a[now][i];
            if (!du[i]) q.push(i);
        }
    }
    return sum==0;
}
int main()
{
    n=read();
    for (int i=1;i<=n;i++)
    {
        scanf("%s",s[i]+1);
        insert(s[i]);
    }
    for (int i=1;i<=n;i++)
    {
        clear();
        query(s[i]);
        if (judge()) ans[++cnt]=i;
    }
    printf("%d\n",cnt);
    for (int i=1;i<=cnt;i++)
        printf("%s\n",s[ans[i]]+1);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值