有趣题。

首先"分成若干个互不相干的子串"是子游戏的定义,可以用 SG 函数处理。

然而接下来试着打了半个多小时的表,没有找到任何规律。

但是发现 SG 函数的状态转移是很简单的。一般 SG 函数难以优化的点在于 \(\text{MEX}\) 操作不太平易近人,这道题中让你取 \(\text{MEX}\) 的只有 26 个数完全可以暴力去做。

进一步地,我们想到这道题中能够成为”子游戏“的子区间是很有限的,对于删去某个字符形成的若干段字符串,只有这些串的前缀后缀是需要计算 SG 函数的。

所以我们只需要按照这些串的长度从小到大计算 SG 值即可。

TLE submission

然而事情并不是那么美好,如果没注意好实现细节直接拿 std::map 维护 SG 值会多 \(\log\),复杂度达到了 \(O(n|\Sigma|^2\log n)\)。

于是重构了一遍代码,考虑按右端点和串长度双关键字的顺序计算这些区间,并且用合适的数组替换掉数据结构,复杂度 \(O(n|\Sigma|^2)\)。

这道题实现还是很有技巧的,应该要有 implementation 的标签。

#include <bits/stdc++.h>
using namespace std;
const int N=100003;
char s[N];
int n,q;
int a[N][26],fa[N][26];
int b[N][26],fb[N][26];
int f[N],cur[26];
int calc(int l,int r){
if(l>r) return 0;
unsigned int st=0;
for(int c=0;c<26;++c){
int pl=a[l][c],pr=b[r][c];
if(pl<=pr) st|=1u<<(f[pl]^f[pr]^fa[l][c]^fb[r][c]);
}
return __builtin_ctz(~st);
}
int stk[26];
int main(){
scanf("%s",s+1);
n=strlen(s+1);
for(int c=0;c<26;++c) a[n+1][c]=n+1;
for(int i=n;i;--i){
memcpy(a[i],a[i+1],104);
a[i][s[i]-97]=i;
}
for(int c=0;c<26;++c) b[0][c]=0;
for(int i=1;i<=n;++i){
memcpy(b[i],b[i-1],104);
b[i][s[i]-97]=i;
}
for(int c=0;c<26;++c) stk[c]=c;
for(int i=1;i<=n;++i){
int c=s[i]-97;
f[i]=f[cur[c]]^fb[i-1][c];cur[c]=i;
sort(stk,stk+26,[&](int x,int y){return b[i][x]>b[i][y];});
for(int t=0;t<26;++t){
int o=stk[t];
if(b[i][o]<i) fb[i][o]=calc(b[i][o]+1,i);
}
if(i<n){
int cc=s[i+1]-97;
for(int j=i;j>cur[cc];--j) fa[j][cc]=calc(j,i);
}
}
scanf("%d",&q);
while(q--){
int l,r;
scanf("%d%d",&l,&r);
if(calc(l,r)) puts("Alice");
else puts("Bob");
}
return 0;
}

CF1037G A Game on Strings Sol的更多相关文章

  1. 区块链之智能合约 solidity踩坑 --上篇

    概述 最近在写合约时遇到一些坑,做一下总结: 介绍主要分一下三个方面: 对区块链的简单描述 结合业务场景,编写简单智能合约,时遇到的坑(上篇) assembly 的使用说明(下篇) 正文 进入正题之前 ...

  2. solidity如何拼接字符串?

    当你开始学习使用solidity开发以太坊智能合约之后,很快你会碰到一个问题: 一.在solidity中该如何拼接字符串? 可能你已经试过了,下面的代码试图把两个字符串使用相加的运算符连接起来,但是这 ...

  3. 以太坊和IPFS如何存储数据

    如何将JSON文件存储在IPFS上,并使用Oraclize访问智能合约中的数据呢? 以太坊是一个成熟的区块链,使开发人员能够创建智能合约,在区块链上执行的程序可以由交易触发.人们经常将区块链称为数据库 ...

  4. [Leetcode][Python]43: Multiply Strings

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 43: Multiply Stringshttps://leetcode.co ...

  5. Hacker Rank: Two Strings - thinking in C# 15+ ways

    March 18, 2016 Problem statement: https://www.hackerrank.com/challenges/two-strings/submissions/code ...

  6. StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing the strings?

    StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing t ...

  7. Multiply Strings

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  8. [LeetCode] Add Strings 字符串相加

    Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...

  9. [LeetCode] Encode and Decode Strings 加码解码字符串

    Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...

  10. [LeetCode] Group Shifted Strings 群组偏移字符串

    Given a string, we can "shift" each of its letter to its successive letter, for example: & ...

随机推荐

  1. node报错解决办法

    依次报错: Error: Cannot find module 'gifsicle' Syntax Error: Error: Cannot find module 'imagemin-gifsicl ...

  2. FIR滤波器的设计

    FIR数字滤波器的设计 线性相位FIR滤波器的特点 单位冲激响应:\(h(n),0\leq n\leq N-1\) 系统函数:\(H(z)=\sum_{n=0}^{N-1}h(n)z^{-n}\) 零 ...

  3. 小程序使用svga

     svga 是一种动画格式.不仅可以在 ios,android,flutter,web 上使用,小程序也支持.设计师使用 after effects 或是 animate 进行动画设计.设计师导出工具 ...

  4. 【javascript】关于charCodeAt()方法

    在做算法题目leetcode 2283时,看见某些答案会使用charCodeAt(),因为自己没用过,所以作此纪录 描述在 JavaScript 中,charCodeAt() 是一种字符串方法,用于检 ...

  5. c++中的构造函数,拷贝构造函数和赋值函数

    1.拷贝构造和赋值函数的区别: 1)拷贝构造函数是一个对象初始化一块内存区域,这块内存就是新对象的内存区,而赋值函数是对于一个已经被初始化的对象来进行赋值操作. 2)一般来说在数据成员包含指针对象的时 ...

  6. Python项目案例开发从入门到实战-1.2 Python语法基础

    书籍信息 1.2 Python语法基础 1.2.1 Python数据类型 数值类型 整型(int):浮点型(float):复数(complex),以j或J结尾,如2+3j 字符串 布尔类型 空值,用N ...

  7. Less-7 '))闭合 和 secure_file_priv 配置写入一句话木马

    Less-7使用的文件导出select ... into outfile ....,一个文件上传. mysql安全配置里有一个配置secure_file_priv控制文件的导出导入. secure_f ...

  8. idea 调试小心得

    1.为什么需要Debug 目的:开发过程中 查找或定位错误或者阅读源码 程序运行的结果(4种情况) 情况1:没有任何bug,程序执行正确! 情况2: 运行以后,出现了错误或异常信息.但是通过 日志文件 ...

  9. rosdep初始化顺利进行

    rosdep初始化顺利进行 rosdep初始化需要两条命令 sudo rosdep init rosdep update 但在国内,我们通常会出现因为网络状况访问服务器超时的问题 解决方案就是将资源手 ...

  10. ASP.NET Core Web API Swagger 按标签Tags分组排序显示

    需求 swagger页面按标签Tags分组显示. 没有打标签Tags的接口,默认归到"未分组". 分组内按接口路径排序 说明 为什么没有使用GroupName对接口进行分组? 暂时 ...