BZOJ2213 & LOJ2161 "POI2011 R2 Day1" Difference largest sub-segment and

Topic Portal

https://lydsy.com/JudgeOnline/problem.php?id=2213

https://loj.ac/problem/2161

answer

Do a simple question to relax.

But this question a lot of details, details can be used as a practice of good questions.

Direct handpicked the most frequent letters and letters appear minimal, so that the original sequence is converted into \ (\ pm1 \) sequences make maximum field and on it.

We can scan the sequence when using an array \ (s [i] [j ] \) represented by \ (I \) for the emergence of the least, \ (J \) at most a maximum sub period occurs and, directly update and maintain it.

But this will be a problem, if there is a section \ (i \) this character did not appear, will be \ (s \) Maintenance counted \ (0 \) times so that the wrong answer. We only need to look at the record at the time of updating the current \ (s [i] [j ] \) of a current sub-segment if there is a \ (i \) , does not appear the answer is not updated.

But there will be such a problem. If \ (i \) appears in the first answer substring, then it will not be counted to, and will always believe that did not occur. (Because the largest sub-segment and the greedy tactics with it directly thrown away)

Fortunately, there is only this one case to be wrong, it can be special sentence. I'm lazy directly to the entire string backwards continue to do it again.


Here is the code. Because every string affects only the \ (s \) array \ (26-1 + 26-1 \) elements, so the timethe complexityIs \ (O (50N) \) . (This thing can not call it complexity, which wrote a constant ah)

#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back

template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b , 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b , 1 : 0;}

typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;

template<typename I>
inline void read(I &x) {
    int f = 0, c;
    while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
    x = c & 15;
    while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
    f ? x = -x : 0;
}

const int N = 1000000 + 7;

int n;
int ss[26][26], sp[26][26], gg[26];
char s[N];

inline void work() {
    int ans = 0, bg = 0;
    for (int i = 1; i <= n; ++i) {
        for (int j = 0; j < 26; ++j) if (s[i] - 'a' != j) {
            if (ss[s[i] - 'a'][j]) ss[s[i] - 'a'][j]--, sp[s[i] - 'a'][j] = 1;
            else ss[s[i] - 'a'][j] = 0, sp[s[i] - 'a'][j] = 0;
            sp[s[i] - 'a'][j] && smax(ans, ss[s[i] - 'a'][j]);
        }
        for (int j = 0; j < 26; ++j) if (s[i] - 'a' != j) ++ss[j][s[i] - 'a'], sp[j][s[i] - 'a'] && smax(ans, ss[j][s[i] - 'a']);
    }
    memset(ss, 0, sizeof(ss));
    memset(sp, 0, sizeof(sp));
    for (int i = n; i; --i) {
        for (int j = 0; j < 26; ++j) if (s[i] - 'a' != j) {
            if (ss[s[i] - 'a'][j]) ss[s[i] - 'a'][j]--, sp[s[i] - 'a'][j] = 1;
            else ss[s[i] - 'a'][j] = 0, sp[s[i] - 'a'][j] = 0;
            sp[s[i] - 'a'][j] && smax(ans, ss[s[i] - 'a'][j]);
        }
        for (int j = 0; j < 26; ++j) if (s[i] - 'a' != j) ++ss[j][s[i] - 'a'], sp[j][s[i] - 'a'] && smax(ans, ss[j][s[i] - 'a']);
    printf("%d\n", ans);
}

inline void init() {
    read(n);
    scanf("%s", s + 1);
}

int main() {
#ifdef hzhkk
    freopen("hkk.in", "r", stdin);
#endif
    init();
    work();
    fclose(stdin), fclose(stdout);
    return 0;
}

Guess you like

Origin www.cnblogs.com/hankeke/p/BZOJ2213.html