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. C语言第二次博客作业—分支结构

    一.PTA实验作业 题目1:计算分段函数 1.实验代码 double x,y; scanf("%lf",&x); if(x>=0){ y=sqrt(x); print ...

  2. codevs 1283 等差子序列

    http://codevs.cn/problem/1283/ 题目描述 Description 给一个 1 到 N 的排列{Ai},询问是否存在 1<=p1<p2<p3<p4& ...

  3. 【译】Gradle 的依赖关系处理不当,可能导致你编译异常

    文章 | Ashesh Bharadwaj 翻译 | 承香墨影 授权 承香墨影 翻译.编辑并发布 在 Android Studio 中,Gradle 构建过程对于开发者来说,很大程度上是抽象的.作为一 ...

  4. 读取.properties的内容1

    属性文件方便于项目进行更改,在项目开发中用的也是非常的普遍,在这里就把属性文件的读取通过代码进行一个小结: package com.oyy.test; import java.io.BufferedI ...

  5. [2]十道算法题【Java实现】

    前言 清明不小心就拖了两天没更了-- 这是十道算法题的第二篇了-上一篇回顾:十道简单算法题 最近在回顾以前使用C写过的数据结构和算法的东西,发现自己的算法和数据结构是真的薄弱,现在用Java改写一下, ...

  6. Nagios监控的部署与配置

    [安装Nagios] yum install -y httpd httpd-devel httpd-tools mysql mysql-devel mysql-server php php-devel ...

  7. confluence搭建详情

    Confluence安装&破解&汉化 编辑时间: 2017年7月7日18:01:13 1.介绍 Atlassian Confluence(简称Confluence)是一个专业的wiki ...

  8. HTML的水平居中和垂直居中解决方案

    水平居中:给div设置一个宽度,然后添加margin:0 auto属性 div{ width:200px; margin:0 auto; } 让绝对定位的div居中 div { position: a ...

  9. python利用文件对话框获取文件路径

    一.单文件 python3: import tkinter as tk from tkinter import filedialog root = tk.Tk() root.withdraw() fi ...

  10. Python 爬虫性能相关

    性能相关 在编写爬虫时,性能的消耗主要在IO请求中,当单进程单线程模式下请求URL时必然会引起等待,从而使得请求整体变慢. import requests def fetch_async(url): ...