Purple Rain

Purple rain falls in the magic kingdom of Linearland which is a straight, thin peninsula.

On close observation however, Professor Nelson Rogers finds that the purple rain is actually a mix of red and blue raindrops.

In his zeal, he records the location and color of the raindrops in different locations along the peninsula. Looking at the data, Professor Rogers wants to know which part of Linearland had the “least” purple rain.

After some thought, he decides to model this problem as follows. Divide the peninsula into n sections and number them west to east from 1 to n. Then, describe the raindrops as a sequence of R and B, depending on whether the rainfall in each section is primarily red or blue. Finally, find a subsequence of contiguous sections where the difference between the number of R and the number of B is maximized.

Input

The input consists of a single line containing a string of n characters (1 ≤ n ≤ 105 ), describing the color of the raindrops in sections 1 to n. It is guaranteed that the string consists of uppercase ASCII letters ‘R’ and ‘B’ only.

Output

Print, on a single line, two space-separated integers that describe the starting and ending positions of the part of Linearland that had the least purple rain. These two numbers should describe an inclusive range; both numbers you print describe sections included in the range. If there are multiple possible answers, print the one that has the westernmost starting section. If there are multiple answers with the same westernmost starting section, print the one with the westernmost ending section.

Sample Input and Output

BBRRBRRBRB 3 7

BBRBBRRB 1 5

连续用两次最长子序列就好了

#include<bits/stdc++.h>
using namespace std;
 
#define cas(t) int t; scanf("%d",&t);
 
const int maxn = 1e6 + 10;
int a[maxn];
int sz;
 
void maxs(int &beg,int &en,int &maxsum){
    int now,p;
    beg = en = p = 1;
    now = maxsum = a[1];
    for(int i = 2;i <= sz;i ++){
        if(now + a[i] < a[i]){
            now = a[i];
            p = i;
        }
        else {
            now += a[i];
        }
        if(maxsum < now){
            maxsum = now;
            beg = p;
            en = i;
        }
    }
}
 
int main()
{
    string s;
    while(cin >> s){
        sz = 0;
        for(int i = 0;i < s.size();i ++){
            if(s[i] == 'R') a[++sz] = 1;
            else a[++ sz] = -1;
        }
        int beg,en,maxsum;
        maxs(beg,en,maxsum);
        sz = 0;
        for(int i = 0;i < s.size();i ++){
            if(s[i] == 'B') a[++ sz] = 1;
            else a[++ sz] = -1;
        }
        int beg1,en1,maxsum1;
        maxs(beg1,en1,maxsum1);
        if(maxsum < maxsum1)
            printf("%d %d\n",beg1,en1);
        else if(maxsum == maxsum1){
            if(beg1 < beg) printf("%d %d\n",beg1,en1);
            else if(beg1 == beg)
                printf("%d %d\n",beg1,min(en,en1));
            else printf("%d %d\n",beg,en);
        }
        else printf("%d %d\n",beg,en);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_40543696/article/details/89047124