Description

You are given n strings s1, s2, ..., sn consisting of characters 0 and 1. m operations are performed, on each of them you concatenate two existing strings into a new one. On the i-th operation the concatenation saisbi is saved into a new string sn + i (the operations are numbered starting from 1). After each operation you need to find the maximum positive integer k such that all possible strings consisting of 0 and 1 of length k (there are 2k such strings) are substrings of the new string. If there is no such k, print 0.

解题报告

这题对于合并的两个串的操作,新串对两个合并的串连边,由于每一次长度最多翻一倍,也就是最长为 \(2^{100}\) ,所以 \(k\) 长度也不超过100,所以可以分治处理,那么新串就可以只存新串的长度为100的部分,也就是说 左边部分的后k个和右边部分的前k个需要单独算贡献,内部的贡献直接分治处理,枚举 \(k\),然后直接哈希字符串判断出现次数,并记录串的个数是否等于 \(2^k\) 即可

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <map>
#define RG register
#define il inline
#define iter iterator
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
const int N=205;
string s[N][2];int len[N],n,ls[N],rs[N],m,tot=0;
map<string,int>re;bool vis[N];
void dfs(int x,int k){
if(vis[x])return ;vis[x]=true;
if(!rs[x]){
for(int i=0;i+k<=len[x];i++){
string w=s[x][0].substr(i,k);
if(!re[w]){
re[w]++;tot++;
}
}
return ;
}
string S=s[ls[x]][1]+s[rs[x]][0];
int li=S.size();
for(int i=0;i+k<=li;i++){
string w=S.substr(i,k);
if(!re[w]){
re[w]++;tot++;
}
}
dfs(ls[x],k);dfs(rs[x],k);
}
bool check(int k,int x){
memset(vis,0,sizeof(vis));re.clear();tot=0;
dfs(x,k);
if(tot==(1<<k))return true;
return false;
}
void work()
{
scanf("%d",&n);
for(int i=1;i<=n;i++){
cin>>s[i][0];
s[i][1]=s[i][0];
ls[i]=rs[i]=0;
len[i]=s[i][0].size();
}
scanf("%d",&m);
for(int i=n+1;i<=n+m;i++){
scanf("%d%d",&ls[i],&rs[i]);
s[i][0]=s[ls[i]][0];
if(len[ls[i]]<=100)s[i][0]=s[i][0]+s[rs[i]][0];
if(s[i][0].size()>=100)s[i][0]=s[i][0].substr(0,100);
s[i][1]=s[ls[i]][1];
if(len[rs[i]]<=100)s[i][1]=s[i][1]+s[rs[i]][1];
if(s[i][1].size()>=100)s[i][1]=s[i][1].substr(s[i][1].size()-100,100);
int k=1;
for(k=1;k<=100;k++){
if(!check(k,i))break;
}
printf("%d\n",k-1);
}
} int main()
{
work();
return 0;
}

Codeforces Round #438 D. Huge Strings的更多相关文章

  1. Codeforces Round #438 (Div.1+Div.2) 总结

    本来兴致勃勃的想乘着这一次上紫,于是很早很早的到了机房 但是好像并没有什么用,反而rating-=47 Codeforces Round #438(Div.1+Div.2) 今天就这样匆匆的总结一下, ...

  2. [Codeforces Round #438][Codeforces 868D. Huge Strings]

    题目链接:868D - Huge Strings 题目大意:有\(n\)个字符串,\(m\)次操作,每次操作把两个字符串拼在一起,并询问这个新串的价值.定义一个新串的价值\(k\)为:最大的\(k\) ...

  3. 【Codeforces Round 438 A B C D 四个题】

    题目所在比赛的地址在这里呀 A. Bark to Unlock ·述大意:       输入一个目标串.然后输入n(1<=n<=100)个串,询问是否可以通过这些串收尾相接或者它本身拼出目 ...

  4. D. Huge Strings Codeforces Round #438 by Sberbank and Barcelona Bootcamp (Div. 1 + Div. 2 combined)

    http://codeforces.com/contest/868/problem/D 优化:两个串合并 原有状态+ 第一个串的尾部&第二个串的头部的状态 串变为第一个串的头部&第二个 ...

  5. Educational Codeforces Round 12 C. Simple Strings 贪心

    C. Simple Strings 题目连接: http://www.codeforces.com/contest/665/problem/C Description zscoder loves si ...

  6. Codeforces Round #438 B. Race Against Time

    Description Have you ever tried to explain to the coordinator, why it is eight hours to the contest ...

  7. Codeforces Round #438 C. Qualification Rounds

    Description Snark and Philip are preparing the problemset for the upcoming pre-qualification round f ...

  8. Codeforces Round #313 D. Equivalent Strings(DFS)

    D. Equivalent Strings time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  9. Codeforces Round #438 by Sberbank and Barcelona Bootcamp (Div. 1 + Div. 2 combined)

    A. Bark to Unlock 题目链接:http://codeforces.com/contest/868/problem/A 题目意思:密码是两个字符组成的,现在你有n个由两个字符组成的字符串 ...

随机推荐

  1. 支付宝sdk集成,报系统繁忙 请稍后再试(ALI64)

    移动快捷支付,往往需要集成支付宝的sdk,集成的过程相对简单,只要按照支付宝的文档,进行操作一般不会出问题.            下面主要说明一下,集成sdk后报"系统繁忙 请稍后再试(A ...

  2. android 时间获取以及时间格式化

    Android中获取系统时间有多种方法,可分为Java中Calendar类获取,java.util.date类实现,还有android中Time实现 现总结如下: 方法一: void getTime1 ...

  3. 2017 清北济南考前刷题Day 3 afternoon

    期望得分:100+40+100=240 实际得分:100+40+100=240 将每个联通块的贡献乘起来就是答案 如果一个联通块的边数>点数 ,那么无解 如果边数=点数,那么贡献是 2 如果边数 ...

  4. 简单介绍 CPU 的工作原理

    1.内部架构 CPU 的根本任务就是执行指令,对计算机来说最终都是一串由 0 和 1 组成的序列.CPU 从逻辑上可以划分成 3 个模块,分别是控制单元.运算单元和存储单元 .其内部架构如下: [1] ...

  5. TP框架关于模版的使用技巧

    1.

  6. python入门(6)输入和输出

    python入门(6)输入和输出 输出 >>> print 'hello, world' >>> print 'The quick brown fox', 'jum ...

  7. 前端学习之jquery

    前端学习之jquery 1.   什么是jQuery对象? jQuery对象就是通过jQuery包装DOM对象后产生的对象.jQuery对象是jQuery独有的.如果一个对象是jQuery对象,那么它 ...

  8. 安卓手机USB共享网络给PC上网

    开端 哈哈,最近我又发现了一个校园网的漏洞,但是只能手机连接,于是就想手机连接之后通过usb共享给电脑上网. 在手机上连接校园网WiFi,开启USB网络共享并且连接电脑之后,却发现电脑十分的卡顿!CP ...

  9. 1.4WEB API 路由配置及参数传输

    在没有添加路由时,webapi 的访问比较恶心,访问的时候是没有接口方法的.如果定义了同样参数的方法,它就傻傻的分不清的,为了解决这个问题,我们加上路由吧. 这是生成的api帮助文档列表,可以看到下面 ...

  10. 【原创】公司各个阶段 CTO 需要做什么?(下篇)

    假设一个公司发展有以下几个阶段: 0 :创始阶段: 0.5 :有产品但无管理阶段: 1 :经过 1年的发展初步稳定的阶段: 1+ :稳步发展阶段. 上一篇文章中,我们聊了公司在初创阶段,CTO 需要做 ...