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.

Input

The first line contains single integer n (1 ≤ n ≤ 100) — the number of strings. The next n lines contain strings s1, s2, ..., sn (1 ≤ |si| ≤ 100), one per line. The total length of strings is not greater than 100.

The next line contains single integer m (1 ≤ m ≤ 100) — the number of operations. m lines follow, each of them contains two integers aiabd bi (1 ≤ ai, bi ≤ n + i - 1) — the number of strings that are concatenated to form sn + i.

Output

Print m lines, each should contain one integer — the answer to the question after the corresponding operation.

Example
input
5
01
10
101
11111
0
3
1 2
6 5
4 4
output
1
2
0
Note

On the first operation, a new string "0110" is created. For k = 1 the two possible binary strings of length k are "0" and "1", they are substrings of the new string. For k = 2 and greater there exist strings of length k that do not appear in this string (for k = 2 such string is "00"). So the answer is 1.

On the second operation the string "01100" is created. Now all strings of length k = 2 are present.

On the third operation the string "1111111111" is created. There is no zero, so the answer is 0.

题意:给定n个01字符串,进行m次操作,每次将Sa,Sb,两个字符串合并成为新串Sn+i

求:对于每个新和成的字符串,存在数字k,满足长度为k的01字符全排列都存在于新和成的字符串的子串中,询问k的最大值

字符串 a 与 b 合并为字符串 ab,得到的 k(ab) 要么为 k(a),要么为 k(b),要么就是新的更大的数。 如果是新的数,那就肯定是合并的中间部分产生的。

也就是说,当前的字符串 s,如果它在后面还能有新的贡献,那就只能是两端与其它字符串合并产生的。 所以如果字符串长度过大,我们可以直接删去中间部分,因为中部部分在后面是不会产生贡献了

实际发现,k的范围其实很小(不知道为什么)

在k很小的情况下,暴力枚举全排列再判断就行

如果要k比较大,似乎有分治解法?

但网上很多题解都写了(1<<k),而且是int,据我所知,这个肯定会爆

然而都AC了,说明有某种结论(或者数据太水)

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
string s[];
int n,m,ans[];
int zyys(int x)
{int i,j,l;
for (i=;i<=;i++)
{
for (j=;j<(<<i);j++)
{
string myys;
for (l=;l<i;l++)
if (j&(<<l)) myys+='';
else myys+='';
if (s[x].find(myys)==-) return i-;
}
}
}
int main()
{int i,x,y;
cin>>n;
for (i=;i<=n;i++)
{
cin>>s[i];
}
cin>>m;
for (i=n+;i<=n+m;i++)
{
scanf("%d%d",&x,&y);
s[i]=s[x]+s[y];
if (s[i].length()>)
s[i]=s[i].substr(,)+s[i].substr(s[i].length()-,);
ans[i]=max(ans[x],max(ans[y],zyys(i)));
printf("%d\n",ans[i]);
}
}

codeforces868D Huge Strings的更多相关文章

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

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

  2. Codeforces Round #438 D. Huge Strings

    Description You are given n strings s1, s2, ..., sn consisting of characters 0 and 1. m operations a ...

  3. Codeforces 868D Huge Strings - 位运算 - 暴力

    You are given n strings s1, s2, ..., sn consisting of characters 0 and 1. m operations are performed ...

  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. Delphi 预编译指令

    <Delphi下深入Windows核心编程>(附录A Delphi编译指令说明)Delphi快速高小的编译器主要来自Object PASCAL的严谨,使用Delphi随时都在与编译器交流, ...

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

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

  7. Delphi编译选项

    编译选项的设置,称为“开关指令”,其中大部分值为布尔类型 一.代码生成(Code generation)1.Optimization  优化代码,默认true2.Stack frames  生成过程/ ...

  8. Delphi编译指令说明

    Delphi快速高效的编译器主要来自Object PASCAL的严谨,使用Delphi随时都在与编译器交流,大部分情况下不需要干涉编译器的运行,但是有时也需要对编译器进行必要的设置. ******** ...

  9. Python之code对象与pyc文件(三)

    上一节:Python之code对象与pyc文件(二) 向pyc写入字符串 在了解Python如何将字符串写入到pyc文件的机制之前,我们先来了解一下结构体WFILE: marshal.c typede ...

随机推荐

  1. 02_LInux的目录结构_我的Linux之路

    前两节已经教大家怎么在虚拟机安装Linux系统 这一节我们来学习Linux的目录结构,讲一下linux的整个系统架构,提前熟悉一下Linux 在Linux或Unix系统中有一个非常重要的概念,就是一切 ...

  2. 20162321王彪-实验二-Java面向对象程序设计

    实验二Java面向对象程序设计 实验内容一 初步掌握单元测试和TDD 什么是单元测试:单元测试时开发者编写的一小段代码,用于检测被测代码的一个很小的,很明确的功能是否正确.执行单元测试,是为了证明某段 ...

  3. 20145237 《Java程序设计》第4周学习总结

    20145237 <Java程序设计>第4周学习总结 教材学习内容总结 一. 继承  . 定义:面向对象中,涉及对象过多时容易出现重复行为,为了避免这种情况,就可把重复的部分写成父类,由子 ...

  4. openlayers调用瓦片地图分析

    网上有诸多资料介绍openlayers如何调用百度地图或者是天地图等常见互联网地图,本文作者使用的是不是常见的互联网瓦片,现将调用过程进行整理与大家分享. 首先,openlayers就不赘述了(官网: ...

  5. MSSQL 2000 错误823恢复

    一.故障描述 MSSQL Server 2000 附加数据库错误823,附加数据库失败.数据库没有备份,不能通过备份恢复数据库,急需恢复数据库中的数据. 二.故障分析SQL Server数据库 823 ...

  6. vue 手机端开发 小商铺 添加购物车 以及结算 功能

    这个功能绕了我一天!!!            对 就是这个功能  一系列相关联的  四处相关联 现在加班 没时间更 过两天在更

  7. java:多层文件夹情况下,判断文件夹下是否有文件夹,并获取到没有文件夹的名字的方法

    业务问题案例 在公司遇到的一个问题,本以为很小很好解决,没想到花了一下午时间.图给的是文件路径,page1下有10个文件夹,每个有的有文件夹或者文件,要求得到page1下(即:123456789,10 ...

  8. Linux的打印rpm包的详细信息的shell脚本

    #!/bin/bash # list a content summary of a number of RPM packages # USAGE: showrpm rpmfile1 rpmfile2 ...

  9. Vue2学习小记-给Vue2路由导航钩子和axios拦截器做个封装

    1.写在前面 最近在学习Vue2,遇到有些页面请求数据需要用户登录权限.服务器响应不符预期的问题,但是总不能每个页面都做单独处理吧,于是想到axios提供了拦截器这个好东西,再于是就出现了本文. 2. ...

  10. 新概念英语(1-105)Full Of Mistakes

    Lesson 105 Full of mistakes 错误百出 Listen to the tape then answer this question. What was Sandra's pre ...