[Usaco2012 Dec]First! BZOJ3012

分析:

其实我们可以很容易的想到,如果一个串是另一个串的子串,那么必定长的那个串不可能是字典序最小的串。其次,如果一个串为了使他成为字典序最小的串儿出现了矛盾的情况,那么也不可能是字典序最小的串。那么,按照规则建出trie树,之后在枚举每个字符串,在枚举的同时建图,判断是否有环,用拓扑排序解决。

附上代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
#define N 30005
struct node
{
    int son[27],flag;
}a[N*10];
struct no
{
    int to,next;
}e[N];
int head[30],cnt,ans[N],in1[N],tot=1,n;
char s[N][305];
void add(int x,int y)
{
    e[cnt].to=y;
    e[cnt].next=head[x];
    head[x]=cnt++;
    in1[y]++;
}
void insert(int x)
{
    int rt=1;
    int len=strlen(s[x]);
    for(int i=0;i<len;i++)
    {
        int idx=s[x][i]-'a'+1;
        if(!a[rt].son[idx])a[rt].son[idx]=++tot;
        rt=a[rt].son[idx];
    }
    a[rt].flag=1;
}
bool tsort()
{
    queue <int>q;
    for(int i=1;i<=26;i++)if(!in1[i])q.push(i);
    while(!q.empty())
    {
        int x=q.front();q.pop();
        for(int i=head[x];i!=-1;i=e[i].next)
        {
            int to1=e[i].to;
            in1[to1]--;
            if(!in1[to1])q.push(to1);
        }
    }
    for(int i=1;i<=26;i++)if(in1[i])return 0;
    return 1;
}
bool check(int x)
{
    int rt=1;
    int len=strlen(s[x]);
    for(int i=0;i<len;i++)
    {
        if(a[rt].flag)return 0;
        int idx=s[x][i]-'a'+1;
        for(int j=1;j<=26;j++)
        {
            if(j!=idx&&a[rt].son[j])add(idx,j);
        }
        rt=a[rt].son[idx];
    }
    return tsort();
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%s",&s[i]);
        insert(i);
    }
    for(int i=1;i<=n;i++)
    {
        memset(head,-1,sizeof(head));
        memset(in1,0,sizeof(in1));cnt=0;
        if(!check(i))continue;
        ans[++ans[0]]=i;
    }
    printf("%d\n",ans[0]);
    for(int i=1;i<=ans[0];i++)
    {
        printf("%s\n",s[ans[i]]);
    }
    return 0;
}

  

posted @   Winniechen  阅读(218)  评论(0编辑  收藏  举报
编辑推荐:
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
阅读排行:
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· 一个基于 .NET 开源免费的异地组网和内网穿透工具
· 《HelloGitHub》第 108 期
· Windows桌面应用自动更新解决方案SharpUpdater5发布
· 我的家庭实验室服务器集群硬件清单
点击右上角即可分享
微信分享提示