时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
Polycarpus analyzes a string called abracadabra. This string is constructed using the following algorithm:

On the first step the string consists of a single character “a”.
On the k-th step Polycarpus concatenates two copies of the string obtained on the (k - 1)-th step, while inserting the k-th character of the alphabet between them. Polycarpus uses the alphabet that consists of lowercase Latin letters and digits (a total of 36 characters). The alphabet characters are numbered like this: the 1-st character is “a”, the 2-nd — “b”, …, the 26-th — “z”, the 27-th — “0”, the 28-th — “1”, …, the 36-th — “9”.
Let’s have a closer look at the algorithm. On the second step Polycarpus will concatenate two strings “a” and insert the character “b” between them, resulting in “aba” string. The third step will transform it into “abacaba”, and the fourth one - into “abacabadabacaba”. Thus, the string constructed on the k-th step will consist of 2k - 1 characters.

Polycarpus wrote down the string he got after 30 steps of the given algorithm and chose two non-empty substrings of it. Your task is to find the length of the longest common substring of the two substrings selected by Polycarpus.

A substring s[i… j] (1 ≤ i ≤ j ≤ |s|) of string s = s1s2… s|s| is a string sisi + 1… sj. For example, substring s[2…4] of string s = “abacaba” equals “bac”. The string is its own substring.

The longest common substring of two strings s and t is the longest string that is a substring of both s and t. For example, the longest common substring of “contest” and “systemtesting” is string “test”. There can be several common substrings of maximum length.

输入描述:
The input consists of a single line containing four integers l1, r1, l2, r2 (1 ≤ li ≤ ri ≤ 109, i = 1, 2). The numbers are separated by single spaces. li and ri give the indices of the first and the last characters of the i-th chosen substring, correspondingly (i = 1, 2). The characters of string abracadabra are numbered starting from 1.

输出描述:
Print a single number — the length of the longest common substring of the given strings. If there are no common substrings, print 0.

输入
样例1

3 6 1 4

样例2
1 1 4 4

输出
样例1

2

样例2

0

题目大意:对于2^k-1长的字符串有如下规律
k=1时,a
k=2时,aba
k=3时,abacaba
k=4时,abacabadabacaba
以此类推……
给定两个区间l1-r1,l2-r2,求两个区间里的最长公共序列的长度

思路:核心:分治、分类讨论
不妨假设l1<l2,即第一个区间的左端点恒在最左。
如果区间a包含于区间b,最长子序列就是a的长度,特判。
利用回文串的性质,如果串串的左端点在右半边,将其向左移动整个区间的一半长度之后是等价的,abacaba中[5,7]等价于[1,3]。这样就将两个串的左端点放在左半边进行处理,方便许多。

剩下要讨论的情况如下,先看情况2,这时将区间继续缩小就可以归到情况1和情况3了。

情况1和3本质其实一样,最大值的出现无非两个情况,重叠的片段或者分为两段,最大值必在其中一段,分治一波。

#include  <bits/stdc++.h>
using namespace std;typedef long long ll;int deal(int l1,int r1,int l2,int r2,int k)//区间长度2^k
{if(l1>r1||l2>r2)    return 0;if(l1>l2)   {swap(l1,l2);swap(r1,r2);}//把第一个区间放左边if(r2<=r1)  return r2-l2+1;//如果第二个区间包含在第一个区间内if(l1==l2)  return r1-l1+1;//第一个区间包含在第二个区间内int len=1<<k;//k从30开始,保证两个区间变换后左端点一定在左半边if(l2>len)  return deal(l1,r1,l2-len,r2-len,k);//左移if(r1<len && r2<len ) return deal(l1,r1,l2,r2,k-1);//都在左半边,区间减半int ans=max(deal(l1,r1,l2,len-1,k),deal(l1,r1,1,r2-len,k));//分治,l1,l2在左半边,r1r2在右半边return max(r1-l2+1,ans);//最大值要么是重叠的部分,要么是分治之后其中一边的最大值
}
int main()
{ios::sync_with_stdio(false);int l1,l2,r1,r2;cin>>l1>>r1>>l2>>r2;cout<<deal(l1,r1,l2,r2,30)<<endl;
}

Abracadabra相关推荐

  1. The Sandbox 推出 BLOND:ISH 的 ABRACADABRA,一场迷幻的限时音乐会体验

    ABRACADABRA!这是加拿大 DJ.唱片艺术家.环保活动家.流媒体/技术迷.加密迷.企业家兼魔术师 BLOND:ISH 的全新限时音乐会体验! 我们的 Alpha 第 2 季驻场 DJ 回来了! ...

  2. Python标准库——collections模块的Counter类

    更多16 最近在看一本名叫<Python Algorithm: Mastering Basic Algorithms in the Python Language>的书,刚好看到提到这个C ...

  3. Python记录-基础语法入门

    # -*- coding: utf-8 -*- #数字计算 a=1 b=2 print(a+b) print(a*b) print((a+b)/1) #浮点数 print((a+b)//2) ##保留 ...

  4. python基础数据实例_Python基本数据类型及实例详解

    Python 中的变量不需要声明.每个变量在使用前都必须赋值,变量赋值以后该变量才会被创建. 在 Python 中,变量就是变量,它没有类型,我们所说的"类型"是变量所指的内存中对 ...

  5. 【Python3_基础系列_006】Python3-set-集合

    一.set集合的方法 set不是特别常用,但是set的一些特性可以方便处理一些特殊情况. 集合(set)是一个无序不重复元素的序列. 可以使用大括号 { } 或者 set() 函数创建集合,注意:创建 ...

  6. JavaScript最全编码规范

    转载: JavaScript最全编码规范 类型 ●基本类型:访问基本类型时,应该直接操作类型值 ●string ●number ●boolean ●null ●undefined var foo = ...

  7. python标准库学习4

    >>> os.environ["HOME"] 'C:\\Users\\Administrator'>>> os.getcwd() #获得当前的目 ...

  8. Python Data Structures

    1. list 2. stack 3. queue 4. tuple 5. sequence 6. set 7. dict # -*- coding: utf-8 -*- # 添加中文注释 ''' C ...

  9. 技术图文:集合技术在求解算法题中的应用

    背景 前段时间,在知识星球立了一个Flag,这是总结Leetcode刷题的第四篇图文. 理论部分 HashSet C# 语言中 HashSet<T> 是包含不重复项的无序列表,称为&quo ...

最新文章

  1. C++中使用new和delete运算符实现二维数组的操作
  2. Qt Creator使用文本编辑宏
  3. python编码效率高吗_【原创】杠精的日常-讨论python快排的效率
  4. win7 activemq_带有骆驼,ActiveMQ,Elasticsearch的关键HL7用例
  5. HTMLCSS编码规范
  6. 正则表达式验证IP和端口格式的正确性
  7. Codeforces 466E Information Graph
  8. 上月用得好好的支付宝获取月账单的Java接口,月初突然返回“入参不合法”的解决方法
  9. 苹果mac休眠快捷键_「苹果电脑技巧」MAC快捷键(2018更新版)
  10. LTE终端分类-LTE UE category
  11. TCGA 亚型突变负荷代码
  12. Win11系统默认英文字体怎么修改成为中文
  13. FTP 错误 550 Failed to change directory 记录贴
  14. CS224d lecture 14札记
  15. ArcBlock 将出席以太坊经典峰会 | ABT 预告
  16. AD画PCB如何做矩形槽孔
  17. 中控煤化工丨大型煤化工智能工厂高效精准生产,到底有多牛?
  18. 对话框处理TAB按键事件的三种方法
  19. python中关于圆的代码
  20. mysql 设计动态字段_数据库设计中动态列的设计方法

热门文章

  1. 互联网老炮儿谈用户,怎一个精字了得
  2. 2015广州强网杯 致敬经典
  3. 2.1立即数的判断方法一
  4. vue过滤器使用方法
  5. 2016年8月26日 星期五 --出埃及记 Exodus 16:27
  6. 使用设计模式出任CEO迎娶白富美(3)--一番对单例模式的讲解让老板熨帖
  7. 手把手教你用 VuePress 快速搭建个人技术博客~
  8. 支撑马蜂窝「双11」营销大战背后的技术架构
  9. 【热血传奇】 怪物添加(上)
  10. 4行Python代码监测每行程序的运行时间和空间消耗