首先每个串都必须是$S$的子序列,否则无解。

按长度从小到大依次考虑每个串,如果它两边都不能放,那么无解。

如果能放一边,那么放进去,把待定的全部放入另一边。

如果两边都能放,那么看看能否待定,如果不能则把它和待定的分别放入两边。

时间复杂度$O(nm)$。

#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 4005
int n,i,j,x,y,len[N],b[N],f[2][N],cnt[2],q[N],t;char a[N][N];
inline bool cmp(int x,int y){return len[x]<len[y];}
inline bool check(int x,int y){
if(len[x]>len[y])return 0;
for(int i=j=0;i<len[x];i++){
while(j<len[y]&&a[x][i]!=a[y][j])j++;
if(j==len[y])return 0;
j++;
}
return 1;
}
int main(){
scanf("%d",&n);
for(i=0;i<=n;i++){
scanf("%s",a[i]);
len[i]=strlen(a[i]);
b[i]=i;
if(!check(i,0))return puts("impossible"),0;
}
len[0]=0;
std::sort(b+1,b+n+1,cmp);
for(i=1;i<=n;i++){
x=check(f[0][cnt[0]],b[i]);
y=check(f[1][cnt[1]],b[i]);
if(!x&&!y)return puts("impossible"),0;
if(x&&!y){
f[0][++cnt[0]]=b[i];
for(j=1;j<=t;j++)f[1][++cnt[1]]=q[j];
t=0;
}
if(!x&&y){
f[1][++cnt[1]]=b[i];
for(j=1;j<=t;j++)f[0][++cnt[0]]=q[j];
t=0;
}
if(x&&y){
if(check(q[t],b[i]))q[++t]=b[i];
else{
f[0][++cnt[0]]=b[i];
for(j=1;j<=t;j++)f[1][++cnt[1]]=q[j];
t=0;
}
}
}
for(j=1;j<=t;j++)f[1][++cnt[1]]=q[j];
printf("%d %d\n",cnt[0],cnt[1]);
for(i=1;i<=cnt[0];i++)puts(a[f[0][i]]);
for(i=1;i<=cnt[1];i++)puts(a[f[1][i]]);
return 0;
}

  

BZOJ4110 : [Wf2015]Evolution in Parallel的更多相关文章

  1. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  2. The evolution of cluster scheduler architectures--转

    原文地址:http://www.firmament.io/blog/scheduler-architectures.html cluster schedulers are an important c ...

  3. A Pattern Language for Parallel Application Programming

    A Pattern Language for Parallel Application Programming Berna L. Massingill, Timothy G. Mattson, Bev ...

  4. 共线性图 | Alluvial Diagrams | Parallel plot | Parallel Coordinates Plot

    最近有个需求需要画如下的图: 这些图的核心意思是一样的,就是connection,把不同的数据连到一起. 文章里把这图叫做共线性图,是按功能命名的,Google里搜不到. 搜到类似的,这个图叫 Par ...

  5. .Net多线程编程—System.Threading.Tasks.Parallel

    System.Threading.Tasks.Parallel类提供了Parallel.Invoke,Parallel.For,Parallel.ForEach这三个静态方法. 1 Parallel. ...

  6. Java 8函数编程轻松入门(五)并行化(parallel)

    1.并发与并行的区别 并发: 一个时间段内有几个程序都处于已启动到运行完毕之间,且这几个程序都是在同一个处理机上运行.但在任一个时刻点只有一个程序在处理机上运行 并行: 在同一个时刻,多核处理多个任务 ...

  7. Parallel并行之乱用

    关于Parallel我也不细说了,一则微软封装的很好用,二来介绍这个的遍地都是. 我要说的是,要想成为一个优秀的标题党,一定要把重点放到别的地方,为了节省大家阅读时间,我先把结论说了,然后再慢慢从头说 ...

  8. 代码的坏味道(12)——平行继承体系(Parallel Inheritance Hierarchies)

    坏味道--平行继承体系(Parallel Inheritance Hierarchies) 平行继承体系(Parallel Inheritance Hierarchies) 其实是 霰弹式修改(Sho ...

  9. 多线程之任务: Task 基础, 多任务并行执行, 并行运算(Parallel)

    Task - 基于线程池的任务(在 System.Threading.Tasks 命名空间下) 多 Task 的并行执行 Parallel - 并行计算(在 System.Threading.Task ...

随机推荐

  1. Django环境配置

    Django安装 #安装最新版本的Django $ pip install django #或者指定安装版本 pip install -v django==1.7.1 项目创建 $ django-ad ...

  2. .net学习之Attribute特性和EF关键知识点

    一.Attribute特性/标签1.Attribute用来对类.属性.方法等标注额外的信息,贴一个标签简单的说,定制特性Attribute,本质上就是一个类,它为目标元素提供关联附加信息,并在运行时以 ...

  3. python调用系统命令popen、system

    python调用Shell脚本,有两种方法:os.system(cmd)或os.popen(cmd),前者返回值是脚本的退出状态码,后者的返回值是脚本执行过程中的输出内容.所以说一般我们认为popen ...

  4. 在source insight中集成astyle

    转自:http://www.cnblogs.com/xuxm2007/archive/2013/04/06/3002390.html 好吧,我有代码格式的强迫症,代码不整齐,我看的都头疼,之前一直喜欢 ...

  5. Debian 的 preinst, postinst, prerm, 和 postrm 脚本

    转自:http://jianjian.blog.51cto.com/35031/395468 这些是软件包安装前后自动运行的可执行脚本. 统称为控制文件, 是 Deian 软件包的"控制&q ...

  6. HR外包系统 - 账款

    01 账款-服务费,注意多币种以及收款对象和联系人的设置 02 代收代付,注意多币种以及收款对象和联系人的设置 03 账单要保存服务费计算的整个过程,便于后面出报表和数据分析 04 出报表时,要考虑 ...

  7. Create a Qt Widget Based Application—Windows

    This turtorial describes how to use Qt Creator to create a small Qt application, Text Finder. It is ...

  8. Codeforces Round #369 (Div. 2) C. Coloring Trees DP

    C. Coloring Trees   ZS the Coder and Chris the Baboon has arrived at Udayland! They walked in the pa ...

  9. loj 1032 数位dp

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1032 思路:数位dp, 采用记忆化搜索, dp[pos][pre][have] 表示 ...

  10. autoprefixer

    自动化补全工具,在写兼容的css样式的时候,自动补全-webkit,-moz等 sublime和websotrm上都可以安装此工具.