zoukankan      html  css  js  c++  java
  • BZOJ2213 & LOJ2161 「POI2011 R2 Day1」Difference 最大子段和

    题目传送门

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

    https://loj.ac/problem/2161

    题解

    做一道简单题来放松一下。

    不过这道题细节挺多的,可以作为一道练细节的好题。

    直接钦定出现最多的字母和出现最少的字母,这样把原序列转化成 (pm1) 的序列做最大字段和就可以了。

    我们可以在扫这个序列的时候用一个数组 (s[i][j]) 表示以 (i) 为出现最少的,(j) 为出现最多的的最大子段和,直接更新维护就可以了。

    但是这样会有一个问题,如果有一段区间 (i) 这个字符根本没有出现,会被 (s) 的维护算作 (0) 次从而使答案错误。我们只需要在更新的时候记录一下当前 (s[i][j]) 的当前一个子段是否出现了 (i),未出现就不更新答案。

    但是这样还会有一个问题。如果 (i) 出现在答案子串的第一位,那么就不会被统计到,而会一直认为没有出现。(因为最大子段和的贪心策略直接把它跟扔掉了)

    所幸就只有这一种情况会出错,可以特判一下。我比较懒就直接把整个串倒着继续做了一遍。


    下面是代码。由于字符串的每一位只会影响到 (s) 数组的 (26-1+26-1) 个元素,因此时间复杂度(O(50n))。(这个东西不能叫复杂度吧,里面写了常数啊)

    #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
    ", 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;
    }
    
  • 相关阅读:
    TSQL语句
    约束
    数据库创建
    递归
    函数
    结构体
    集合
    jquery中的select
    正则表达式
    多表单提交
  • 原文地址:https://www.cnblogs.com/hankeke/p/BZOJ2213.html
Copyright © 2011-2022 走看看