[luogu P3065] [USACO12DEC]第一!First!

题目描述

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个字符串,问哪些串能在特定的字母顺序中字典序最小。

输入输出格式

输入格式:

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

输出格式:

  • 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: 复制

4
omm
moo
mom
ommnom
输出样例#1: 复制

2
omm
mom

说明

The example from the problem statement.

Only "omm" and "mom" can be ordered first.

来点不是很难又不是很水的题目。

这一题最开始的想法就是建一棵字典树trie,然后,对于每一个单词,沿着字典树中他的路径走下去。

那怎么判断是否可行?如果按照贪心的想法,比如当前的节点优先级设为剩下(除去前几个字母)最高的,这样显然会有反例。

那么,我们想,安排字母的顺序,优先级,我们想到了topo排序。

由于每一个节点下面,非当前路径上的点的优先级小于路径上的点,所以就可以建一条边。

在这里可以直接用邻接矩阵,更方便,且效率也没差到哪里(因为可能有很多边)。

然后,就进行topo排序了,如果可行就可以了。

还有需要注意的是,比如有两个字符串:

wzz

wzzlihai

那么,wzzlihai也不能以某种顺序排到第一位。

那这个怎么判呢?在每个单词结束的时候都在结束点打个“结束”标记。

然后询问时,如果路径上某一个点(非最后一个)上有“结束”标记,则return 0。

code:

 #pragma GCC optimize(2)
 #include <cstdio>
 #include <cstring>
 #include <algorithm>
 #include <iostream>
 #include <string>
 #include <queue>
 #define ms(a,x) memset(a,x,sizeof a)
 typedef long long LL;
 namespace fastIO {
     #define puc(c) putchar(c)
     inline int read() {
         ,f=; char ch=getchar();
         ') {
             if (ch=='-') f=-f;
             ch=getchar();
         }
         ') {
             x=(x<<)+(x<<)+ch-';
             ch=getchar();
         }
         return x*f;
     }
     template <) {
         T f=; char ch=getchar();
         ') {
             if (ch=='-') f=-f;
             ch=getchar();
         }
         ') {
             x=(x<<)+(x<<)+ch-';
             ch=getchar();
         }
         x*=f;
     }
     ];
     template <class T> inline void write(T x) {
         ) {
             puc(');
             return;
         }
         ) {
             x=-x;
             puc('-');
         }
         ; x; x/=) w[++cnt]=x%;
         );
     }
     inline void newline() {
         puc('\n');
     }
     inline void newblank() {
         puc(' ');
     }
 }
 namespace OJ{
     void Online_Judge() {
         #ifndef ONLINE_JUDGE
             freopen("in.txt","r",stdin);
             freopen("out.txt","w",stdout);
         #endif
     }
 }
 using std::string;
 using std::queue;
 ,L=,A=;
 int n,cnt,len[N]; bool vis[N]; string s[N]; char ss[L];
 int tot,f[A][A],dg[N];
 queue <int> q;
 #define TrieNode node
 class TrieNode {
     private:
         bool end; node *ch[A];
     public:
         node() {
             end=,ms(ch,);
         }
         inline bool topo() {
             while (!q.empty()) q.pop();
             ; i<A; ++i) {
                 ; j<A; ++j) {
                     if (f[i][j]) ++dg[j];
                 }
             }
             ; i<A; ++i) {
                 ) q.push(i);
             }
             ;
             for (int x; !q.empty(); ) {
                 x=q.front(),q.pop();
                 ; i<A; i++) {
                     if (f[x][i]) {
                         --dg[i];
                         ) q.push(i);
                     }
                 }
             }
             ; i<A; ++i) {
                 ) ;
             }
             ;
         }
         inline void insert(node *u,char a[],int l) {
             ,x; i<l; ++i) {
                 x=a[i]-'a';
                 ) {
                     u->ch[x]=new node();
                 }
                 u=u->ch[x];
             }
             u->end=;
         }
         inline bool reply(node *u,char a[],int l) {
             ms(f,),ms(dg,);
             ,x; i<l; ++i) {
                 x=a[i]-'a';
                 &&u->ch[x]->end) ;
                 ; j<; ++j) {
                     &&j!=x) f[x][j]=;
                 }
                 u=u->ch[x];
             }
             return topo();
         }
 }t,*rot;
 int main() {
     OJ::Online_Judge();
     scanf(,rot=new node();
     ; i<=n; ++i) {
         scanf("%s",ss),len[i]=strlen(ss);
         s[i]="";
         ; j<len[i]; ++j) {
             s[i]=s[i]+ss[j];
         }
         t.insert(rot,ss,len[i]);
     }
     ; i<=n; ++i) {
         ; j<len[i]; ++j) {
             ss[j]=s[i][j];
         }
         cnt+=vis[i]=t.reply(rot,ss,len[i]);
     }
     printf("%d\n",cnt);
     ; i<=n; ++i) {
         if (vis[i]) {
             ; j<len[i]; ++j) {
                 putchar(s[i][j]);
             }
             putchar('\n');
         }
     }
     ;
 }

[luogu P3065] [USACO12DEC]第一!First!的更多相关文章

  1. Luogu P3065 [USACO12DEC]第一!First!【字典树/拓扑排序】By cellur925

    题意:给你许多字符串,你可以改变字母序大小,问有哪些字符串可能成为字典序最小的字符串. 我们考虑把这些字符串都塞到\(trie\)树上.之后检索每一个字符串的时候,我们看和他同一层的地方是否有字符,如 ...

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

    P3065 [USACO12DEC]第一!First! 题目链接:https://www.luogu.org/problemnew/show/P3065 题目描述 Bessie一直在研究字符串.她发现 ...

  3. P3065 [USACO12DEC]第一!First!

    题目描述 Bessie has been playing with strings again. She found that by changing the order of the alphabe ...

  4. [USACO12DEC]第一!First! (Trie树,拓扑排序)

    题目链接 Solution 感觉比较巧的题啊... 考虑几点: 可以交换无数次字母表,即字母表可以为任意形态. 对于以其他字符串为前缀的字符串,我们可以直接舍去. 因为此时它所包含的前缀的字典序绝对比 ...

  5. [USACO12DEC]第一!First!(字典树,拓扑排序)

    [USACO12DEC]第一!First! 题目描述 Bessie has been playing with strings again. She found that by changing th ...

  6. [bzoj3012][luogu3065][USACO12DEC][第一!First!] (trie+拓扑排序判环)

    题目描述 Bessie has been playing with strings again. She found that by changing the order of the alphabe ...

  7. luogu P3065 first——trie树相关

    题目描述 Bessie has been playing with strings again. She found that by changing the order of the alphabe ...

  8. 【[USACO12DEC]第一!First!】

    一个串不能成为第一的情况有两种 另外一个单词是它的前缀 在分配字母表大小关系的时候出现了矛盾的情况 第一种很好判断,一旦我们在一个单词没有匹配完之前遇到一个结束标志,那么就说明另外一个单词是它的前缀 ...

  9. 【luogu P3063 [USACO12DEC]牛奶的路由Milk Routing】 题解

    题目链接:https://www.luogu.org/problemnew/show/P3063#sub 我很好奇这道题为什么没被收入SPFA好题 #include <cstdio> #i ...

随机推荐

  1. centos7安装git

    1.安装git依赖包 yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUti ...

  2. 内存泄漏(I)

    Block 解决内存泄漏 使用 weakSelf 进行解决 NSTimer 的内存泄漏与解决方案 内存泄漏

  3. decimal(19,6)什么意思

    decimal(19,6)什么意思 数字长度19位,精确到小数点后6位例如0.123456 mysql中varchar(50)最多能存多少个汉字 首先要确定mysql版本4.0版本以下,varchar ...

  4. PAT (Basic Level) Practice (中文)1004 成绩排名 (20 分)

    题目链接:https://pintia.cn/problem-sets/994805260223102976/problems/994805321640296448 #include <iost ...

  5. vue强制刷新组件

    <component v-if="hackReset"></component>(组件名称) data:hackReset (事件执行) this.hack ...

  6. WIN10下微信崩溃(已经是最新版)的解决方法

    微信运行错误---------------------------你的微信崩溃次数较多,建议使用最新版本,点击"确定"到官网(http://pc.weixin.qq.com/)下载 ...

  7. [Python数据挖掘]第6章、电力窃漏电用户自动识别

    一.背景与挖掘目标 相关背景自查 二.分析方法与过程 1.EDA(探索性数据分析) 1.分布分析 2.周期性分析 2.数据预处理 1.数据清洗 过滤非居民用电数据,过滤节假日用电数据(节假日用电量明显 ...

  8. rem的在vue项目中使用配置,,浏览器的兼容性之Mate标签

    在vue中配置rem 位置:在APP.vue的script中,在export default之外 (()=>{ let winW = document.documentElement.clien ...

  9. SQL中IN与EXISTS的区别

    1.IN子句中的子查询只能返回一个字段,不允许返回多个字段,而EXISTS可以返回多个字段 2.IN返回的是某字段的值,而EXISTS返回的则是True或False,EXISTS子句存在符合条件的结果 ...

  10. stm32库函数建工程和使用Keil自带库建工程有没有区别?发现了同样的程序在两种情况下keil自带库可以运行的情况,不知是什么原因

    我使用库函数建的工程(非Keil自带库),为了实现SPI对Si24r1芯片数据的读写,以验证stm32是否可以和si24r1能够正常通信,发现使用库函数建的工程程序不能通过,读出来的数据和写的数据不一 ...