BZOJ 3012: [Usaco2012 Dec]First! 字典树 + tarjan
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].
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.
题解: 比较简单的一道题吧,考试的时候大家都几乎一眼切.
将所有串插进字典树
依次枚举每个串,将其定义为字典序最小的串.
那么,就要满足对于字典树中每一个分叉,必须保证当前串该字符大小要小于该分叉其余所有字符.
那么,这样就构建出了一些大小关系.
如果大小关系出现了环,说明无解.
我是用 tarjan 找环来判断的.
#include<bits/stdc++.h>
#define maxn 1000003
using namespace std;
char str[maxn],strtot[maxn];
int lentot,scc,flag,pp=0;
int st[maxn],ed[maxn],C[30][30],vis[maxn],vised[maxn],pre[maxn],low[maxn];
void setIO(string s)
{
string in=s+".in";
string out=s+".out";
freopen(in.c_str(),"r",stdin);
freopen(out.c_str(),"w",stdout);
}
vector<int>G[maxn],answer,tag[maxn];
stack<int>S;
struct Trie
{
int cnt;
int ch[maxn][30];
void ins(char p[],int o)
{
int len=strlen(p+1),cur=0;
for(int i=1;i<=len;++i)
{
int c=p[i]-'a';
if(!ch[cur][c])
{
ch[cur][c]=++cnt;
G[cur].push_back(c);
}
cur=ch[cur][c];
}
tag[cur].push_back(o);
}
}trie;
void tarjan(int u)
{
S.push(u);
vised[u]=1;
pre[u]=low[u]=++scc;
for(int i=0;i<27;++i)
{
if(u==i || !vis[i] || !C[u][i]) continue;
if(!vised[i]) tarjan(i), low[u]=min(low[u], low[i]);
else if(vised[i]==1) low[u]=min(low[u],pre[i]);
}
if(low[u]==pre[u])
{
int cc=0;
for(;;)
{
int x=S.top();S.pop();
++cc;
vised[x]=-1;
if(cc>1) flag=1;
if(x==u) break;
}
} }
bool check()
{
flag=scc=0;
while(!S.empty())S.pop();
for(int i=0;i<27;++i) vised[i]=low[i]=pre[i]=0;
for(int i=0;i<27;++i)
{
if(!vis[i]) continue;
if(!vised[i]) tarjan(i);
}
return flag^1;
}
void dfs(int u,int depth)
{
if(tag[u].size())
{
if(check())
{
for(int j=0,sz=tag[u].size();j<sz;++j) answer.push_back(tag[u][j]);
}
return;
}
for(int i=0;i<27;++i)
{
// 有延申出去的单词
if(trie.ch[u][i]) // 当前为 u
{
++vis[i];
int y=u;
for(int j=0,sz=G[u].size();j<sz;++j)
{
if(G[u][j]!=i)
{
++C[i][G[u][j]];
}
}
dfs(trie.ch[u][i],depth+1);
for(int j=0,sz=G[u].size();j<sz;++j)
{
if(G[u][j]!=i) --C[i][G[y][j]];
}
--vis[i];
}
}
}
int main()
{
// setIO("ok");
int n;
scanf("%d",&n);
for(int i=1;i<=n;++i)
{
scanf("%s",str+1);
trie.ins(str,i);
int v=strlen(str+1);
st[i]=lentot+1;
for(int j=1;j<=v;++j) strtot[++lentot]=str[j];
ed[i]=lentot;
}
dfs(0,0);
printf("%d\n",answer.size());
sort(answer.begin(),answer.end());
for(int i=0,sz=answer.size();i<sz;++i)
{
for(int j=st[answer[i]];j<=ed[answer[i]];++j) printf("%c",strtot[j]);
printf("\n");
}
return 0;
}
BZOJ 3012: [Usaco2012 Dec]First! 字典树 + tarjan的更多相关文章
- bzoj 3012: [Usaco2012 Dec]First! Trie+拓扑排序
题目大意: 给定n个总长不超过m的互不相同的字符串,现在你可以任意指定字符之间的大小关系.问有多少个串可能成为字典序最小的串,并输出这些串.n <= 30,000 , m <= 300,0 ...
- BZOJ_3012_[Usaco2012 Dec]First!_trie树+拓扑排序
BZOJ_3012_[Usaco2012 Dec]First!_trie树+拓扑排序 题意: 给定n个总长不超过m的互不相同的字符串,现在你可以任意指定字符之间的大小关系.问有多少个串可能成为字典序最 ...
- BZOJ 3011: [Usaco2012 Dec]Running Away From the Barn( dfs序 + 主席树 )
子树操作, dfs序即可.然后计算<=L就直接在可持久化线段树上查询 -------------------------------------------------------------- ...
- 【BZOJ3012】[Usaco2012 Dec]First! Trie树+拓补排序
[BZOJ3012][Usaco2012 Dec]First! Description Bessie has been playing with strings again. She found th ...
- BZOJ 1212 L语言(DP+字典树)
求能被理解的最长前缀. 很显然的dp.令dp[i]=true,表示前缀i能理解.否则不能理解.那么dp[i+len]=dp[i]=true,当s[len]能匹配str[i,i+len]. 由于模式串长 ...
- BZOJ 4260 Codechef REBXOR(字典树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4260 [题目大意] 给出一个数列,请找出两段连续且不相交的数段,使得其分别异或和的和 ...
- 「Usaco2012 Dec」第一(字典树+拓扑排序)
(我恨字符串) 惯例化简题目:给定n个字符串,可以改变字符的相对大小(在字典序中的大小),问:字符串i是否能成为最小的字符串(字典序) 解题过程: 首先你可以预处理出来26的全排列然后暴力然后你只要用 ...
- BZOJ 4260 Codechef REBXOR (区间异或和最值) (01字典树+DP)
<题目链接> 题目大意:给定一个序列,现在求出两段不相交的区间异或和的最大值. 解题分析: 区间异或问题首先想到01字典树.利用前缀.后缀建树,并且利用异或的性质,相同的两个数异或变成0, ...
- BZOJ 3483 SGU505 Prefixes and suffixes(字典树+可持久化线段树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3483 [题目大意] 给出一些串,同时给出m对前缀后缀,询问有多少串满足给出的前缀后缀模 ...
随机推荐
- Void 参数
在C程序中如果在声明函数的时候如果没有任何参数那么需要将参数定义为void以此来限定此函数不可传递任何参数,如果不进行限定让参数表默认为空其意义是可以传递任何参数,这个问题的由来实际上是由于要兼容早期 ...
- Grails里的集成测试代码试例
测试的命令,3和2不一样了,要找找.. User.groovy package com.grailsinaction class User { String loginId String passwo ...
- HDU 2348
DFS+博弈. 假设存在两数(x,y),且x<y.对于(x+ky,y)k>=2,只能转移向两种状态(x+y,y),或者(x,y).而对于(x+y,y)只能向(x,y)转移,那么,可知,无论 ...
- Qt移动应用开发(一):适配不同的屏幕
Qt移动应用开发(一):适配不同的屏幕 到眼下为止.Qt5.3已经出现非常长一段时间了.而且已经有一些应用使用Qt进行构建了.我自己也完毕了第一款使用Qt构建的手机游戏<吃药了>.那么接下 ...
- [Angular] Remove divs to Preserve Style and Layout with ng-container in Angular
The Angular <ng-container> is a grouping element that doesn't interfere with styles or layout ...
- android weight(权重)的详细分析
首先要明确权重分配的是那些空间? 权重是依照比例分配屏幕的剩余空间 对这句话不理解的能够看下图 假如我们希望剩余的空间平分给空间1 和空间2 , 我们分别在2个控件的设置android:layout_ ...
- 保护眼睛-eclipse黑色背景设置
eclipse中java编辑器颜色改动,适合程序员人群: 长时间编码,眼睛是有非常大负担的,特别是使用eclipse,它自带的java编辑器背景色是刺眼的白色.代码颜色基本是黑色,这样一个编辑器里白色 ...
- hdu 4850 字符串构造---欧拉回路构造序列 递归+非递归实现
http://acm.hdu.edu.cn/showproblem.php? pid=4850 题意:构造长度为n的字符序列.使得>=4的子串仅仅出现一次 事实上最长仅仅能构造出来26^4+4- ...
- oc31--new实现
// // main.m // new方法实现原理 #import <Foundation/Foundation.h> #import "Person.h" int m ...
- 87.Ext_菜单组件_Ext.menu.Menu
转自:https://blog.csdn.net/lms1256012967/article/details/52574921 菜单组件常用配置: /* Ext.menu.Menu主要配置项表: it ...