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. class AClass<E extends Comparable>与class AClass<E extends Comaprable<E>>有什么区别?

    new ArrayList<>()与new ArrayList()一样 都是为了做限定用的 如果不了解你可以看API 这个Comparable里面有一个方法compareTo(T o) 如 ...

  2. SUN平台服务器光纤共享存储互斥失败如何恢复数据?

    服务器数据恢复故障描述: 服务器最初的设计思路为将两台SPARC SOLARIS系统通过光纤交换机共享同一存储作为CLUSTER使用,正常情况下A服务器工作,当A服务器发生故障宕机后即可将其关机然后开 ...

  3. Echarts柱状图实现不同颜色渐变色

    第一次写文,只是想记录一下自己平时发现的小功能,这篇主要是实现echarts柱状图,每个柱子实现不同颜色的渐变色,也是第一次接触echarts,后台使用ssm,前台是extjs,直接上效果图 直接上j ...

  4. Python习题(第一课)

    想了想其他的太简单了,还是不放了,剩三题吧. 一.完美立方 编写一个程序,对任给的正整数N (N≤100),寻找所有的四元组(a, b, c, d),使得a^3= b^3 + c^3 + d^3,其中 ...

  5. java之多态详解

    前言 什么叫多态?多态就是一种事物可以有多种表现形式 多态三要素 1.被动方必须有继承关系 2.子类一般都要重写父类方法 3.必须将主动方的功能函数的参数设置为 被动方父类的类型 举个例子司机开车 假 ...

  6. LeetCode & Q1-Two Sum-Easy

    Array Hash Table Question Given an array of integers, return indices of the two numbers such that th ...

  7. Python内置函数(46)——format

    英文文档: format(value[, format_spec]) Convert a value to a "formatted" representation, as con ...

  8. Mego开发文档 - 数据属性生成值

    数据属性生成值 该功能用于在数据插入或更新时为指定属性生成期望的值,Mego提供了非常灵活的实现方式以满足各种数据提交时的自动赋值问题. 生成值目的及模式 在Mego中生成值的目的一定是插入数据或更新 ...

  9. 阿里云API网关(3)快速入门(调用 API)

    网关指南: https://help.aliyun.com/document_detail/29487.html?spm=5176.doc48835.6.550.23Oqbl 网关控制台: https ...

  10. background属性的学习整理转述

    前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! background我们一般用到的的属性有: background-attachment:背景(图片)是否 ...