Codeforces 524F And Yet Another Bracket Sequence 哈希

And Yet Another Bracket Sequence

枚举起点, 增加的(肯定在最前面, 增加的)肯定在最后面, 比字典序用hash, 卡了自然溢出。。

#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0);

using namespace std;

const int N = 2e6 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
const double eps = 1e-8;
const double PI = acos(-1);

template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;}

const int B = 23333;
int n;
char s[N];
int a[N];

LL hs[N], powB[N], powL[N], powR[N];

int cntL[N], cntR[N];

int Log[N];
struct ST {
    int dp[N][21], ty;
    void build(int n, int b[], int _ty) {
        ty = _ty;
        for(int i = -(Log[0]=-1); i < N; i++)
        Log[i] = Log[i - 1] + ((i & (i - 1)) == 0);
        for(int i = 1; i <= n; i++) dp[i][0] = ty * b[i];
        for(int j = 1; j <= Log[n]; j++)
            for(int i = 1; i+(1<<j)-1 <= n; i++)
                dp[i][j] = max(dp[i][j-1], dp[i+(1<<(j-1))][j-1]);
    }
    inline int query(int x, int y) {
        int k = Log[y - x + 1];
        return ty * max(dp[x][k], dp[y-(1<<k)+1][k]);
    }
} rmq;

inline LL getHash(int L, int R) {
    return ((hs[R] - hs[L - 1] * powB[R - L + 1] % mod) + mod) % mod;
}

inline LL getPosHash(int i, int len) {
    if(len <= cntL[i]) return powL[len];
    if(len <= cntL[i] + n) return (powL[cntL[i]] * powB[len - cntL[i]] % mod + getHash(i, i + len - cntL[i] - 1)) % mod;
    return (powR[len - cntL[i] - n] + getHash(i, i + n - 1) * powB[len - cntL[i] - n] % mod + powL[cntL[i]] * powB[len - cntL[i]] % mod) % mod;
}

inline char getch(int i, int p) {
    if(p <= cntL[i]) return ‘(‘;
    else if(p <= cntL[i] + n) return s[i + p - cntL[i] - 1];
    return ‘)‘;
}

bool check(int p1, int p2, int len) {
    int low = 1, high = len, mid, p = -1;
    while(low <= high) {
        mid = low + high >> 1;
        if(getPosHash(p1, mid) != getPosHash(p2, mid)) p = mid, high = mid - 1;
        else low = mid + 1;
    }
    if(p == -1) return false;
    return getch(p1, p) < getch(p2, p);
}

int main() {
    for(int i = powB[0] = 1; i < N; i++) powB[i] = powB[i - 1] * B % mod;
    for(int i = 1; i < N; i++) powL[i] = (powL[i - 1] * B + ‘(‘) % mod;
    for(int i = 1; i < N; i++) powR[i] = (powR[i - 1] * B + ‘)‘) % mod;
    scanf("%s", s + 1);
    n = strlen(s + 1);
    for(int i = 1; i <= n; i++) s[i + n] = s[i];
    for(int i = 1; i <= 2 * n; i++) hs[i] = (hs[i - 1] * B + s[i]) % mod;
    for(int i = 1; i <= 2 * n; i++) a[i] = a[i - 1] + (s[i] == ‘(‘ ? 1 : -1);
    rmq.build(2 * n, a, -1);
    int need = inf;
    for(int i = 1; i <= n; i++) {
        int mn = rmq.query(i, i + n - 1);
        if(mn < a[i - 1]) cntL[i] = a[i - 1] - mn;
        if(a[i + n - 1] + cntL[i] >= a[i - 1]) cntR[i] = a[i + n - 1] + cntL[i] - a[i - 1];
        if(cntL[i] + cntR[i] < need) need = cntL[i] + cntR[i];
    }
    int who = -1;
    for(int i = 1; i <= n; i++) {
        if(cntL[i] + cntR[i] > need) continue;
        if(who == -1) who = i;
        else {
            if(check(i, who, need + n)) who = i;
        }
    }
    while(cntL[who]--) putchar(‘(‘);
    for(int i = who; i < who + n; i++) putchar(s[i]);
    while(cntR[who]--) putchar(‘)‘);
    puts("");
    return 0;
}

/**/

原文地址:https://www.cnblogs.com/CJLHY/p/10990538.html

时间: 2024-08-06 06:00:39

Codeforces 524F And Yet Another Bracket Sequence 哈希的相关文章

CodeForces - 524F And Yet Another Bracket Sequence

题面在这里! (会考完之后休闲休闲2333) 可以发现,如果把一个串中"()"自动删除,最后剩的一定是形如"))))....))(((..((("这样的串,然后我们多加进去的括号的个数就是剩的这个串的长度.. 然鹅这个题首先要求的是最后总长度最小,并且我们可以观察发现把最后一个循环位移到最前面是会使一对")("相抵消的. 所以我们最后的最优答案一定是排除已经匹配的括号之后只剩一种括号的串,我们枚举一下循环位移了多少(循环位移之后一定是一个原串的后

【Codeforces 3D】Least Cost Bracket Sequence

Codeforces 3 D 题意:有一个括号序列,其中一些位置是问号,把第\(i\)个问号改成(需要\(a_i\)的代价,把它改成)需要\(b_i\)的代价. 问使得这个括号序列成立所需要的最小代价. 思路1: 这个是正统的贪心. 首先我们假设所有的位置上都是),那么我们在从左向右扫描的途中会发现一些问题. 比如:我们原来的序列是(??),现在假设成了())),那么在第三个字符处就会发现我们的打开的左括号数量为\(-1\),这是肯定不行的,所以我们必须把第二个字符或者第三个字符改成左括号以把左

Codeforces Beta Round #5 C. Longest Regular Bracket Sequence 栈/dp

C. Longest Regular Bracket Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/5/C Description This is yet another problem dealing with regular bracket sequences. We should remind you that a bracket sequence

贪心+stack Codeforces Beta Round #5 C. Longest Regular Bracket Sequence

题目传送门 1 /* 2 题意:求最长括号匹配的长度和它的个数 3 贪心+stack:用栈存放最近的左括号的位置,若是有右括号匹配,则记录它们的长度,更新最大值,可以在O (n)解决 4 详细解释:http://blog.csdn.net/taoxin52/article/details/26012167 5 */ 6 #include <cstdio> 7 #include <algorithm> 8 #include <cstring> 9 #include <

Codeforces 1132A. Regular Bracket Sequence

原题链接:Codeforces 1132A. Regular Bracket Sequence 题目大意:你有\({cnt}_1,{cnt}_2,{cnt}_3,{cnt}_4\)个"((","()",")(","))",问能否将这些字符串组成一个合法的括号序列. 题解:这一道题,很明显的\({cnt}_2\)是不需要管的,对于第三种情况,它并不改变左右括号的数量差,只有第一.四情况改变,那么,很明显\({cnt}_1={cn

Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor

E. Correct Bracket Sequence Editor Recently Polycarp started to develop a text editor that works only with correct bracket sequences (abbreviated as CBS). Note that a bracket sequence is correct if it is possible to get a correct mathematical express

Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 线段树模拟

E. Correct Bracket Sequence Editor Recently Polycarp started to develop a text editor that works only with correct bracket sequences (abbreviated as CBS). Note that a bracket sequence is correct if it is possible to get a correct mathematical express

cf3D Least Cost Bracket Sequence

This is yet another problem on regular bracket sequences. A bracket sequence is called regular, if by inserting "+" and "1" into it we get a correct mathematical expression. For example, sequences "(())()", "()" and

cf670E Correct Bracket Sequence Editor

Recently Polycarp started to develop a text editor that works only with correct bracket sequences (abbreviated as CBS). Note that a bracket sequence is correct if it is possible to get a correct mathematical expression by adding "+"-s and "