CF1037G A Game on Strings Sol
有趣题。
首先"分成若干个互不相干的子串"是子游戏的定义,可以用 SG 函数处理。
然而接下来试着打了半个多小时的表,没有找到任何规律。
但是发现 SG 函数的状态转移是很简单的。一般 SG 函数难以优化的点在于 \(\text{MEX}\) 操作不太平易近人,这道题中让你取 \(\text{MEX}\) 的只有 26 个数完全可以暴力去做。
进一步地,我们想到这道题中能够成为”子游戏“的子区间是很有限的,对于删去某个字符形成的若干段字符串,只有这些串的前缀后缀是需要计算 SG 函数的。
所以我们只需要按照这些串的长度从小到大计算 SG 值即可。
然而事情并不是那么美好,如果没注意好实现细节直接拿 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的更多相关文章
- 区块链之智能合约 solidity踩坑 --上篇
概述 最近在写合约时遇到一些坑,做一下总结: 介绍主要分一下三个方面: 对区块链的简单描述 结合业务场景,编写简单智能合约,时遇到的坑(上篇) assembly 的使用说明(下篇) 正文 进入正题之前 ...
- solidity如何拼接字符串?
当你开始学习使用solidity开发以太坊智能合约之后,很快你会碰到一个问题: 一.在solidity中该如何拼接字符串? 可能你已经试过了,下面的代码试图把两个字符串使用相加的运算符连接起来,但是这 ...
- 以太坊和IPFS如何存储数据
如何将JSON文件存储在IPFS上,并使用Oraclize访问智能合约中的数据呢? 以太坊是一个成熟的区块链,使开发人员能够创建智能合约,在区块链上执行的程序可以由交易触发.人们经常将区块链称为数据库 ...
- [Leetcode][Python]43: Multiply Strings
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 43: Multiply Stringshttps://leetcode.co ...
- Hacker Rank: Two Strings - thinking in C# 15+ ways
March 18, 2016 Problem statement: https://www.hackerrank.com/challenges/two-strings/submissions/code ...
- 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 ...
- Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- [LeetCode] Add Strings 字符串相加
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...
- [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 ...
- [LeetCode] Group Shifted Strings 群组偏移字符串
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
随机推荐
- 关于vmwork中centos7的虚拟机创建
1.准备vmwork软件和centos7的镜像文件 vmwork软件下载地址https://www.vmware.com/cn/products/workstation-pro/workstation ...
- C/C++ 数据结构优先级队列的实现(使用二级指针)
#include <iostream> #include <Windows.h> #include <iomanip> //优先级队列的实现 using names ...
- Hive启动留下的RunJar进程不能使用Kill -9 杀不掉怎么办?
1.问题示例 [Hadoop@master Logs]$ jps 3728 ResourceManager 6976 RunJar 7587 Jps 4277 Master 3095 NameNode ...
- Qt 5.15.2 QTextEdit无法设置新字体的处理方式
首发于我的个人博客:xie-kang.com 博客内有更多文章,欢迎大家访问 原文地址 在使用QT 5.15.2 开发的过程中碰到了件怪事,下列代码无法给QTextEdit选中的文字设置字体: QTe ...
- TP5.1模板循环标签
第一种volist name=assign中的变量名 id=数组中的key offset=开始循环的位置 length=步长 {volist name='list' id='vo' offset='0 ...
- discuz论坛或门户下载的图片无法显示?
discuz论坛或门户下载的图片无法显示? 使用某些插件或者软件(例如火车头采集器,简数采集工具等)的图片下载功能,发现下载成功了后台也有但是前台无法显示,捣鼓了一轮最终才发现是路径的问题. disc ...
- SQL中获取多条记录拼接成字符串
declare @aa nvarchar(200) set @aa=(select STUFF((select ','+LTRIM(list.TempName1) from (select TempN ...
- SpringBoot使用邮件发送
使用场景: 定时任务报错 消息推送 日志报错提醒 1.导入依赖 <dependency> <groupId>org.springframework.boot</group ...
- 简述SpringAOP的实现原理
Spring默认采取的动态代理机制实现AOP,当动态代理不可用时 (代理类无接口)会使用CGlib机制. Spring提供了两种方式来生成代理对象:JDKProxy和Cglib,具体使用哪种方式生 ...
- Kafka + SpringData + (Avro & String) 【Can't convert value of class java.lang.String】问题解决
[1]需求:Kafka 使用 Avero 反序列化时,同时需要对 String 类型的 JSON数据进行反序列化.AvroConfig的配置信息如下: 1 /** 2 * @author zzx 3 ...