codeforces 161C Abracadabra (分治)

编程入门 行业动态 更新时间:2024-10-11 03:15:04

<a href=https://www.elefans.com/category/jswz/34/1770097.html style=codeforces 161C Abracadabra (分治)"/>

codeforces 161C Abracadabra (分治)

 

连接:

题目:C. Abracadabra

time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output

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.

Input

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.

Output

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

Sample test(s) input
3 6 1 4
output
2
input
1 1 4 4
output
0
Note

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 161C Abracadabra (分治)

本文发布于:2024-02-26 02:55:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1701132.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:codeforces   Abracadabra

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!