CF701F String set queries (分块思想+暴力)

很容易想到AC自动机,但是却发现不怎么支持删除

完蛋,怎么办?

思考如何优化暴力

有两种暴力:一种是kmp,一种是trie

trie时间复杂度优秀,但空间不行;

kmp时间不行

那么我们可以互补一下

对于长度小于 \(sqrt\) 的,我们加入 \(trie\) 中,否则暴力 \(kmp\),这样能够维持时间复杂度,因为总长不大

分块思想真妙!

#include <map>
#include <set>
#include <ctime>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
#include <bitset>
#include <cstdio>
#include <cctype>
#include <string>
#include <numeric>
#include <cstring>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std ;
//#define int long long
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define loop(s, v, it) for (s::iterator it = v.begin(); it != v.end(); it++)
#define cont(i, x) for (int i = head[x]; i; i = e[i].nxt)
#define clr(a) memset(a, 0, sizeof(a))
#define ass(a, sum) memset(a, sum, sizeof(a))
#define lowbit(x) (x & -x)
#define all(x) x.begin(), x.end()
#define ub upper_bound
#define lb lower_bound
#define pq priority_queue
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define iv inline void
#define enter cout << endl
#define siz(x) ((int)x.size())
#define file(x) freopen(#x".in", "r", stdin),freopen(#x".out", "w", stdout)
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair <int, int> pii ;
typedef vector <int> vi ;
typedef vector <pii> vii ;
typedef queue <int> qi ;
typedef queue <pii> qii ;
typedef set <int> si ;
typedef map <int, int> mii ;
typedef map <string, int> msi ;
const int N = 300010 ;
const int Sqrt = 500 ;
const int M = N / Sqrt + 10 ;
const int INF = 0x3f3f3f3f ;
const int iinf = 1 << 30 ;
const ll linf = 2e18 ;
const int MOD = 1000000007 ;
const double eps = 1e-7 ;
void print(int x) { cout << x << endl ; exit(0) ; }
void PRINT(string x) { cout << x << endl ; exit(0) ; }
void douout(double x){ printf("%lf\n", x + 0.0000000001) ; }
template <class T> void chmin(T &a, T b) { if (a > b) a = b ; }
template <class T> void chmax(T &a, T b) { if (a < b) a = b ; }
void upd(int &a, int b) { (a += b) %= MOD ; }
void mul(int &a, int b) { a = 1ll * a * b % MOD ; }

char s[N] ;
string st[M] ;
int cost[M] ;
int trie[N][29], value[N], nxt[N] ;
int Q, op, tot = 1, cnt, len ;

signed main(){
//  freopen("test.in", "r", stdin) ;
//  freopen("test.out", "w", stdout) ;
    scanf("%d", &Q) ;
    while (Q--) {
        scanf("%d %s", &op, s) ;
        len = strlen(s) ;
        if (op < 3) {
            if (len <= Sqrt) {
                int now = 1 ;
                rep(i, 0, len - 1) {
                    int c = s[i] - 'a' ;
                    if (!trie[now][c]) trie[now][c] = ++tot ;
                    now = trie[now][c] ;
                }
                value[now] += 3 - 2 * op ;
            } else {
                st[cnt] = "" ;
                rep(j, 0, len - 1) st[cnt] += s[j] ;
                st[cnt] += '{' ;
                cost[cnt] = 3 - 2 * op ;
                cnt++ ;
            }
        } else {
            ll ans = 0 ;
            rep(j, 0, len - 1) {
                int now = 1 ;
                rep(k, j, len - 1) {
                    int c = s[k] - 'a' ;
                    if (!trie[now][c]) break ;
                    now = trie[now][c] ;
                    ans += value[now] ;
                }
            }
        //  cout << ans << endl ;
            rep(id, 0, cnt - 1) {
                int Len = siz(st[id]) - 1 ; // 有一个结尾符
                if (Len > len) continue ;
                nxt[1] = 0 ;
                for (int i = 2, j = 0; i <= Len; i++) {
                    while (j && st[id][i - 1] != st[id][j]) j = nxt[j] ;
                    if (st[id][i - 1] == st[id][j]) j++ ;
                    nxt[i] = j ;
                }
                for (int i = 1, j = 0; i <= len; i++) {
                    while (j && s[i - 1] != st[id][j]) j = nxt[j] ;
                    if (s[i - 1] == st[id][j]) j++ ;
                    if (j == Len) ans += cost[id] ;
                }
            }
            cout << ans << endl ;
        }
    }
    return 0 ;
}

/*
写代码时请注意:
    1.ll?数组大小,边界?数据范围?
    2.精度?
    3.特判?
    4.至少做一些
思考提醒:
    1.最大值最小->二分?
    2.可以贪心么?不行dp可以么
    3.可以优化么
    4.维护区间用什么数据结构?
    5.统计方案是用dp?模了么?
    6.逆向思维?
*/

原文地址:https://www.cnblogs.com/harryhqg/p/10548925.html

时间: 2025-01-03 11:44:45

CF701F String set queries (分块思想+暴力)的相关文章

【BZOJ1257】余数之和(数论分块,暴力)

[BZOJ1257]余数之和(数论分块,暴力) 题解 Description 给出正整数n和k,计算j(n, k)=k mod 1 + k mod 2 + k mod 3 + - + k mod n的值,其中k mod i表示k除以i的余数.例如j(5, 3)=3 mod 1 + 3 mod 2 + 3 mod 3 + 3 mod 4 + 3 mod 5=0+1+0+3+3=7 Input 输入仅一行,包含两个整数n, k. Output 输出仅一行,即j(n, k). Sample Input

数列区间询问中的分块思想

分块算法主要用于给定序列的区间询问问题,能够以较小的时间代价暴力求解,时间复杂度一般在O(n*n^0.5).关键在O(1) 维护好某一区间在增加或者减少一个边界元素所带来的影响.需要注意的就是在更新的区间的时候要先放大在缩小,否则可能出现当前区间左右边界互换的情况,这 个影响某一些题可能没有影响,但是极有可能出错. 时间复杂度:先考虑左边界的时间复杂度,由于分成了sqrt(n)块,而同一块中左标移动的范围最多是sqrt(n),那相邻块跳 转的情况呢?可以虚拟出每块中有至少一个询问进行思考,那么相

CF710F String Set Queries

维护一个字符串集合,支持三种操作: 1.加字符串 2.删字符串 3.查询集合中的所有字符串在给出的模板串中出现的次数 操作数m≤3∗10^5,输入字符串总长度L≤4∗10^6 AC自动机+二进制分组 二进制分组的基本思想是把修改操作按二的次幂分组,遇到修改就在尾部加一个,且与之前的合并(暴力重构),比如之前有23(16+4+2+1)个,加了一个后就变成了24(16+8)个,遇到查询就在每个组内查询,再加起来就好了,所以修改对询问的贡献独立时才能用二进制分组. 这道题对于加的串和删的串分别维护,查

ZOJ 1654 Place the Robots建图思维(分块思想)+二分匹配

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=654 AC一百道水题,不如AC一道难题来的舒服. 题意:一个n*m地图.*代表草地,#代表墙,o代表空地,要再图中的o处放机器人,机器人能够攻击(上下左右)4个方向,攻击范围无限长,并且机器人不能相互攻击,草地不能放置机器人,且机器人的攻击能够穿过草地,可是机器人的攻击不能穿过墙,比方 "   *o#o  "这一行就能够放两个机器人," o*oo

codeforces E - Anya and Cubes 分块处理 暴力搜索

说的是给了n个立方体,立方体从1标号到n,每个立方体上有一个数字, 你有 k 个机会 使得其中 k个数位他们自己的阶乘,(自然使用可以少于k次机会,每个立方体最多被使用1次) ,那么求出你从这n个立方体重选出任意个立方体使得 他们的和为S n<25 可以知道直接使用n去枚举自然受不了, 我们将n个数字分为两部分来算,前面n/2个数字 我们枚举他们使用j次可以得到的数,然后后面的n-n/2个数再次使用这个方法,直接在dfs枚举的时候进行判断S-s 在前一个钟是否出现过,出现过就加起来. 分块处理

哈希冲突[分块(思想)]

题目背景 此题约为NOIP提高组Day2T2难度. 题目描述 众所周知,模数的hash会产生冲突.例如,如果模的数p=7,那么4和11便冲突了. B君对hash冲突很感兴趣.他会给出一个正整数序列value[]. 自然,B君会把这些数据存进hash池.第value[k]会被存进(k%p)这个池.这样就能造成很多冲突. B君会给定许多个p和x,询问在模p时,x这个池内数的总和. 另外,B君会随时更改value[k].每次更改立即生效. 保证1<=p<n1<=p<n1<=p<

hdu2328 Corporate Identity【string库使用】【暴力】【KMP】

Corporate Identity Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3308    Accepted Submission(s): 1228 Problem Description Beside other services, ACM helps companies to clearly state their "cor

CodeForces - 710F:String Set Queries (二进制分组 处理 在线AC自动机)

ou should process m queries over a set D of strings. Each query is one of three kinds: Add a string s to the set D. It is guaranteed that the string s was not added before. Delete a string s from the set D. It is guaranteed that the string s is in th

Codeforces 551E GukiZ and GukiZiana(分块思想)

题目链接 GukiZ and GukiZiana 题目大意:一个数列,支持两个操作.一种是对区间$[l, r]$中的数全部加上$k$,另一种是查询数列中值为$x$的下标的最大值减最小值. $n <= 500000, q <= 50000$ 我一开始的反应是线段树,然后发现自己完全想错了…… 这道题时限10秒,但也很容易超时.我后来是用分块过的. 把序列分成$\sqrt{n}$个块,每个块的大小为$\sqrt{n}$(最后一个块可能因为不能整除的关系可能会小一些) 每个块维护一个值$delta[