# BZOJ2213 & LOJ2161 「POI2011 R2 Day1」Difference 最大子段和

## 题目传送门

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

https://loj.ac/problem/2161

## 题解

#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>
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() {
scanf("%s", s + 1);
}

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

posted @ 2019-09-09 19:27  hankeke303  阅读(249)  评论(0编辑  收藏  举报