有趣题。

首先"分成若干个互不相干的子串"是子游戏的定义,可以用 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. day47-Mysql初识

    1.数据库的演变过程-- 文件存储(不同用户之间数据格式不一致,杂乱)==> 软件开发目录规范(限定了储存的具体位置,不能网络通信)==>数据库 数据库就是一款基于网络通信操作文件的应用程 ...

  2. 替换yum源

    1.yum源进行备份 进入到yum源的配置文件中 执行命令如下:cd /etc/yum.repos.d 将yum源进行备份:mv Centos-Base.repo Centos-Base.repo.b ...

  3. Qt中的渲染

    Qt中3种不同的渲染方式 1 )Qt::AA_UseDesktopOpenGL 使用显卡的openGL库,且要求支持openGL 2.1及以上的版本.因此很多老旧设备是不满足版本要求的(windows ...

  4. mysql数据库用sql语句在指定的一个字段后面添加一个字段

    alert table (新增列的表名)   add (新列名) comment (添加备注)+[after  + 要跟随的字段名]可写可不写 ALTER TABLE ch_poliy_info AD ...

  5. Ipmitool命令之ipmitool user(用户管理)

    常见的用户配置命令: (1)查看用户清单 root@master:~# ipmitool user list 1 ID Name Callin Link Auth IPMI Msg Channel P ...

  6. P4555 [国家集训队]最长双回文串 回文树(回文自动机)简单题

    贴个题目链接:https://www.luogu.org/problem/P4555 题目:输入长度为n的串S,求S的最长双回文子串T,即可将T分为两部分X,Y,(∣X∣,∣Y∣≥1)且X和Y都是回文 ...

  7. mybatisplus数据层标准开发---分页功能

    1.创建一个拦截器类 package com.itheima.config; import com.baomidou.mybatisplus.extension.plugins.MybatisPlus ...

  8. DOM选择器之元素节点选择器

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. Redhat7/CentOS7 网络配置与管理(nmtui、nmcli、GNOME GUI、ifcfg文件、IP命令)

    Redhat7/CentOS7 网络配置与管理(nmtui.nmcli.GNOME GUI.ifcfg文件.IP命令) 背景:作为系统管理员,需要经常处理主机网络问题,而配置与管理网络的方法和工具也有 ...

  10. python自动化测试pyautogui,pywinauto和pywin32组合使用初级

    1 前言PC 端自动化测试使用到的 python 模块主要有 pywinauto.win32gui.pyautogui,主要功能如下: pywinauto:主要使用到 Application 类,用于 ...