洛谷P3065 [USACO12DEC]第一!First!(Trie树+拓扑排序)

P3065 [USACO12DEC]第一!First!

题目链接:https://www.luogu.org/problemnew/show/P3065

题目描述

Bessie一直在研究字符串。她发现,通过改变字母表的顺序,她可以按改变后的字母表来排列字符串(字典序大小排列)。

例如,Bessie发现,对于字符串串“omm”,“moo”,“mom”和“ommnom”,她可以使用标准字母表使“mom”排在第一个(即字典序最小),她也可以使用字母表“abcdefghijklonmpqrstuvwxyz”使得“omm”排在第一个。然而,Bessie想不出任何方法(改变字母表顺序)使得“moo”或“ommnom”排在第一个。

接下来让我们通过重新排列字母表的顺序来计算输入中有哪些字符串可以排在第一个(即字典序最小),从而帮助Bessie。

要计算字符串X和字符串Y按照重新排列过的字母表顺序来排列的顺序,先找到它们第一个不同的字母X[i]与Y[i],按重排后的字母表顺序比较,若X[i]比Y[i]先,则X的字典序比Y小,即X排在Y前;若没有不同的字母,则比较X与Y长度,若X比Y短,则X的字典序比Y小,即X排在Y前。

输入输出格式

输入格式:

 

* 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.

第1行:一个数字N(1 <= N <= 30,000),Bessie正在研究的字符串的数量。

第2~N+1行:每行包含一个非空字符串。所有字符串包含的字符总数不会超过300,000。 输入中的所有字符都是小写字母,即a~z。 输入不包含重复的字符串。

 

输出格式:

 

* 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.

第1行:一个数字K,表示按重排后的字母表顺序排列的字符串有多少可以排在第一个数量。

第2~K+1行:第i+1行包含第i个按重排后的字母表顺序排列后可以排在第一个的字符串。字符串应该按照它们在输入中的顺序来输出。

 

输入输出样例

输入样例#1:
4 
omm 
moo 
mom 
ommnom 
输出样例#1:
2 
omm 
mom 

 

题解:

我们首先可以想到构建一颗Trie树,这样可以帮助我们节省一大波时间。通过分析问题,我们就可以发现,如果一个字符串的字典序要最小,首先其余任何一个字符串都不为它的前缀;并且,对于在同一层的其它点,字符的字典序可以满足最小。如果同时满足这两个条件的话,我们就可以认定它的字典序最小了。

前缀这个问题Trie树很好解决,主要就是如何判断字典序最小呢?这里我们可以用拓扑排序来求解,如果无环,那么就说明它是最小的。

具体做法就是枚举每一个串,假定它的字典序是最小的,然后就在Trie树上面一个一个找,一层一层连边直至末尾结点,最后通过拓扑排序判断一下就行了。

代码如下:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,tot;
int val[1000005],check[30005],vis[30],head[30],in[30],Not[30005];
char s[300005];
string str[30005];
struct Edge{
    int u,v,next;
}e[1000005];
void adde(int u,int v){
    e[tot].u=u;e[tot].v=v;e[tot].next=head[u];head[u]=tot++;
}
struct Trie{
    char node;
    int son,left;
    Trie(){son=left=-1;}
}tre[1000005];
void init(){
    tre[0].son=tre[0].left=-1;tot=1;
}
void insert(int now){
    int j,u=0;
    int l = strlen(s);
    for(int i=0;i<l;i++){
        bool flag=false;
        for(j=tre[u].son;j!=-1;j=tre[j].left){
            if(tre[j].node==s[i]){
                flag=true;
                break ;
            }
        }
        if(!flag){
            j=tot++;
            tre[j].left=tre[u].son;
            tre[u].son=j;
            tre[j].node=s[i];
            tre[j].son=-1;
        }
        u=j;
    }
    val[u]=1;
}
int Use(string S){
    memset(head,-1,sizeof(head));tot=0;
    memset(in,0,sizeof(in));memset(vis,0,sizeof(vis));
    int l=S.length();
    int u=0,j,Next;
    for(int i=0;i<l;i++){
        for(j=tre[u].son;j!=-1;j=tre[j].left){
            int u=S[i]-'a',v=tre[j].node-'a';
            if(u==v){
                Next=j;
                continue ;
            }
            adde(u,v);vis[u]=vis[v]=1;in[v]++;
        }
        u=Next;
        if(val[u]&&i!=l-1) return 0;
    }
    return 1;
}
int top_sort(){
    queue <int> q;
    int cnt = 0,num = 0;
    for(int i=0;i<26;i++) if(!in[i]&&vis[i]) q.push(i),cnt++;
    for(int i=0;i<26;i++) if(vis[i]) num++;
    while(!q.empty()){
        int u=q.front();q.pop();
        for(int i=head[u];i!=-1;i=e[i].next){
            int v=e[i].v;
            if(--in[v]==0) q.push(v),cnt++;
        }
    }
    return cnt==num;
}
int main(){
    cin>>n;
    init();
    for(int i=1;i<=n;i++){
        scanf("%s",s);
        str[i]=s;
        insert(i);
    }
    int ans = 0;
    for(int i=1;i<=n;i++){
        if(Use(str[i])&&top_sort()) check[i]=1,ans++;
    }
    printf("%d\n",ans);
    for(int i=1;i<=n;i++){
        if(check[i]) cout<<str[i]<<'\n';
    }
    return 0;
}

 

posted @ 2019-03-05 20:28  heyuhhh  阅读(178)  评论(0编辑  收藏  举报