连接:http://codeforces.com/problemset/problem/161/C
题目:C. Abracadabra
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.
3 6 1 4
2
1 1 4 4
0
In the first sample the first substring is "acab", the second one is "abac". These two substrings have two longest common substrings "ac" and "ab", but we are only interested in their length — 2.
In the second sample the first substring is "a", the second one is "c". These two substrings don't have any common characters, so the length of their longest common substring is 0.
/* *2^30内的字符中,2^29上的字符一定是唯一的 *这个是分治的关键 *如果最大相同子串包含这个字符,那么最大子串一定是重叠的部分,否则 *它把两个区间分别分成两部分,这两部分可以分别算出重复的最大子串 *然后找出最大值。 *依次类推 */ #include <cstdio> #include <algorithm> using namespace std ; int solver( int l1 , int r1 , int l2 , int r2 , int step = 30 ) { if( l1 > r1 || l2 > r2 ) return 0 ; //结束条件 int ovl = max( l1 , l2 ) ; int ovr = min( r1 , r2 ) ; int overlap = ( (ovl <= ovr) ? (ovr - ovl + 1) : 0 ) ; //算出重叠部分 if( l1 <= l2 && r1 >= r2 || l1 >= l2 && r1 <= r2 ) return overlap ; //第一种情况,包含的情况,直接返回重叠部分 int m = ( 1 << ( step - 1 ) ) ;//取中间值 int res = overlap ; //两个区间分别被中间的字符分成两部分,两两算出重叠的最大部分,取最大值返回。 res = max( res , solver( min( l1 , m ) , min( r1 , m - 1 ) , min( l2 , m ) , min( r2 , m - 1 ) , step - 1 ) ) ; res = max( res , solver( min( l1 , m ) , min( r1 , m - 1 ) , max( l2 , m + 1 ) - m , max( r2 , m ) - m , step - 1 ) ) ; res = max( res , solver( max( l1 , m + 1 ) - m , max( r1 , m ) - m , min( l2 , m ) , min( r2 , m - 1 ) , step - 1 ) ) ; res = max( res , solver( max( l1 , m + 1 ) - m , max( r1 , m ) - m , max( l2 , m + 1 ) - m , max( r2 , m ) - m , step - 1 ) ) ; return res ; } int main() { int l1 , l2 , r1 , r2 ; while( ~scanf("%d%d%d%d" , &l1 , &r1 , &l2 , &r2 ) ) printf("%d\n" , solver( l1 , r1 , l2 , r2 ) ) ; return 0 ; }
相关推荐
Codeforces 题库 101-200 共~500题 codeforces.com版权所有。 程序可提交至该网站评测。
codeforces编程网站预测分数插件
Codeforces 题库 001-100 共~500题 codeforces.com版权所有。 程序可提交至该网站评测。
暴枚最长桌脚的长度$l$,然后长度比$l$长的桌脚全部都要砍掉长度比$l$短的桌脚选择代价前$k$小的砍掉用线段树维护;示例程序 :typedef long l
使用于Google Chrome的Codeforces Enhancer 1.1.2插件安装包。 版本:codeforces enhancer 1.1.2 使用浏览器:Google Chrome
Codeforces 185A - Plant 全测试点49个
codeforces 19 E Fairy 一道比较难的题目的解题报告 推荐阅读
Codeforces global round 10 codes
Codeforces round 678 division 2 codes
Some of the Codeforces problems codes
Codeforces - 1107B. Digital root & 1107C. Brutality(规律 & 贪心)Codeforces - 1107B.
Codeforces round 678 D2_Codeforces_源码
一个Codeforces、牛客竞赛、AtCoder平台的编程竞赛查询插件,ACMer必备.zip
打codeforces的神器
lucifer1004大佬的博客cf上分攻略故里大佬的githubcf思维题刷题数:44- (1421)codeforces 676 div2 A,B done
codeforces-js Codeforces JS
Codeforces Round #723 (Div. 2).md
PDF-CodeForces问题 PDF文件中的CodeForces问题 利用CodeForces提取在这个库中的文件 ,文件夹名代表来自CodeForces网址contests`的ID,每个文件夹包含比赛的问题。 有关CodeForces竞赛的疯狂事实 CodeForces上有937...
codeforces-plugin-sublime-text ... 其中,problemID 是比赛 ID 后跟问题代码,例如 520A、520a、136c... 从 Tools>Build System>Codeforces 中选择构建系统作为“Codeforces”。 在 OS X/Linux 机器上点击
使用 C# + WPF 开发 --- 还在发愁打了那么多场比赛都没有进入首页么? 还在为了前 5 的 hacker 名额阅读千份代码么? 是的,你没有看错! 这是一个 Edu & Div.3 轮 Open hacking 错误代码自动查找器!...