cf 161C 解题报告

cf 161C 解题报告

Description
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 Input
Input
3 6 1 4
Output
2
Input
1 1 4 4
Output
0
Hint
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.

题意:大概是给你一个字符串,他是又以下规则生成的
首先第一步,整个字符串为a,然后有36个字符从a到z到0到9
第二步,首先在前一步得到的字符串后面加一个字符,第二步就+b
然后把前一步得到的字符串再复制一遍添到b后面
比如,第一步是a, 第二步就变成了aba,依此变下去

然后经过30步,会得到一个很长的字符串
问题给你la, ra, lb, rb要求在原串中从la到ra字符复制出来用字符串s1表示
从原串中从lb到rb复制出来用字符串s2表示
要求s1和s2的最长公共子串

解析:
由分析可以知道,原串一定是回文的,那么假设原串的长度为len,如果它的某一个子串只在其左边或者只在其右边,那么我们可以位移。
有以上这个结论,我们就可以通过dfs实现位移,然后保证某一个串完全包含另一个串时,我们就可以返回值了.

//
//  Created by Running Photon on 2015-08-31
//  Copyright (c) 2015 Running Photon. All rights reserved.
//
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <sstream>
#include <set>
#include <vector>
#include <stack>
#define ALL(x) x.begin(), x.end()
#define INS(x) inserter(x, x,begin())
#define ll long long
#define CLR(x) memset(x, 0, sizeof x)
#define MAXN 9999
#define MAXSIZE 10
#define DLEN 4
using namespace std;
const int inf = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int maxn = 1e6 + 10;
const int maxv = 1e3 + 10;
const double eps = 1e-9;


inline int read() {
    char c = getchar();
    int f = 1;
    while(!isdigit(c)) {
    if(c == '-') f = -1;
    c = getchar();
    }
    int x = 0;
    while(isdigit(c)) {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x * f;
}
int dfs(int la, int ra, int lb, int rb, int k) {
    if(k < 0) return 0;
    if(la > ra || lb > rb) return 0;
    //通过交换,保证每次操作的时候a串的左端都在最左边
    if(la > lb) {swap(la, lb); swap(ra,rb);}
    //完全包含时输出
    if(ra >= rb) return rb - lb + 1;
    if(la == lb) return ra - la + 1;
    //当前串的长度
    int len = (1 << k);
    //b串不在当前长度的串之内,那么一定在之后某个生成的串内,通过位移操作使得b串前移
    if(lb > len) return dfs(la, ra, lb-len, rb-len, k);
    //a串和b串都在当前串之内,但是不完全包含,缩小总串进行分割。
    if(ra < len && rb < len) return dfs(la, ra, lb, rb, k - 1);
    //否则,必然存在超过len的串,由前面限制条件只,必然rb > len
    //那么,通过分割,可以把b串分割成两部分,取个最大值即可
    int ans = max(dfs(la, ra, lb, len-1, k), dfs(la, ra, 1, rb-len, k));
    return max(ans, ra - lb + 1);
}
int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
//  freopen("out.txt","w",stdout);
#endif
    int a, b, c, d;
    while(scanf("%d%d%d%d", &a, &b, &c, &d) != EOF){
        printf("%d\n", dfs(a, b, c, d, 30));
    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值