当前位置:网站首页>第十九届浙江省 I. Barbecue
第十九届浙江省 I. Barbecue
2022-07-03 04:37:00 【Snow_raw】
第十九届浙江省 I. Barbecue
题意: 两个同学Putata和Budada在玩一个游戏,一开始给定一个固定长度的字符串,接下去有 m m m次询问,每次询问给定一个范围 [ l , r ] [l,r] [l,r],即从原字符串中截取 [ l , r ] [l,r] [l,r]的部分。从第一个同学Putata开始操作,每次操作必须从字串开头或末尾移除一个字符,如果操作前后字串变成回文串那么操作的同学判负,问每次询问哪个同学会赢?
Tip:
- 如果一开始第一个同学拿到的就是回文串,那么必定输掉比赛。
- 否则第一个同学拿到的就是非回文串,由于如果操作后子串变成回文串那么也算操作的同学输,那么在操作的同学最好的方案就是 非回文 * \implies *非回文
- 这就说明如果游戏没结束即上一次操作的同学结束操作后字串变成 非回文,那么意味着游戏如果始终持续进行除去 第一次操作 ,操作的同学拿到的 始终是非回文串 。那么通过不断操作,最终拿到长度为 2 2 2 的同学 必败 ,因为无论怎么操作,剩下的 1 1 1 个字符必定是 回文 。
- 那么显然有如果第一次操作的同学拿到的是 非回文 并且长度为偶数那么第一次操作的同学必败。如果是 非回文 并且长度为奇数,那么第一次操作的同学必胜。 当然非回文偶数字串有一种特殊情况为 a b a b a b a b abababab abababab,不管拿掉首个字符还是末尾字符都是操作者输,所以同样归类至第一种情况。
- 接下去就是如果判定区间 [ l , r ] [l,r] [l,r]内的字串为回文串,由于题目给到的查询有 1 0 6 10^6 106,所以支持的复杂度即为 O ( 1 ) O(1) O(1)查询。那么我们可以通过字符串 h a s h hash hash和 M a n a c h a r Manachar Manachar算法来进行处理。此处给出字符串 h a s h hash hash做法(学习地址)。
代码:
#include<bits/stdc++.h>
using namespace std;
int n,q;
const int P = 131;
const int N = 1e6+10;
unsigned long long p[N],t[N],t1[N];
unsigned long long query1(int l,int r){
return t[r]-t[l-1]*p[r-l+1];
}
unsigned long long query2(int l,int r){
return t1[l]-t1[r+1]*p[r-l+1];
}
char s[N];
int main(){
scanf("%d%d",&n,&q);
scanf("%s",s+1);
p[0]=1;
t[0]=0;
t1[n+1]=0;
for(int i=1;i<=n;i++){
p[i]=p[i-1]*P;
t[i]=t[i-1]*P+s[i];
}
for(int i=n;i>=1;i--){
t1[i]=t1[i+1]*P+s[i];
}
while(q--){
int l,r;
scanf("%d%d",&l,&r);
if(query1(l,r)==query2(l,r)){
printf("Budada\n");
}
else{
if((r-l+1)%2==0)printf("Budada\n");
else printf("Putata\n");
}
}
return 0;
}
边栏推荐
- Employee attendance management system based on SSM
- 2022 P cylinder filling test content and P cylinder filling simulation test questions
- [pat (basic level) practice] - [simple simulation] 1063 calculate the spectral radius
- FFMpeg example
- JVM原理简介
- arthas watch 抓取入参的某个字段/属性
- 2022 Shandong Province safety officer C certificate examination content and Shandong Province safety officer C certificate examination questions and analysis
- 2022-02-13 (347. Top k high frequency elements)
- Games101 Lesson 9 shading 3 Notes
- [free completion] development of course guidance platform (source code +lunwen)
猜你喜欢

Number of 1 in binary (simple difficulty)

What are the Bluetooth headsets with good sound quality in 2022? Inventory of four high-quality Bluetooth headsets

消息队列(MQ)介绍

C language series - Section 3 - functions

Leetcode simple question: check whether the string is an array prefix

Function introduction of member points mall system

FFMpeg filter

When using the benchmarksql tool to preheat data for kingbasees, execute: select sys_ Prewarm ('ndx_oorder_2 ') error

Why should programmers learn microservice architecture if they want to enter a large factory?

Library management system based on SSM
随机推荐
Kingbasees plug-in KDB of Jincang database_ exists_ expand
Contents of welder (primary) examination and welder (primary) examination in 2022
data2vec! New milestone of unified mode
Kubernetes源码分析(一)
Leetcode simple question: check whether the array is sorted and rotated
Small program animation realizes the running lantern and animation object
Number of 1 in binary (simple difficulty)
Kubernetes source code analysis (I)
After reviewing MySQL for a month, I was stunned when the interviewer of Alibaba asked me
[set theory] binary relationship (definition field | value field | inverse operation | inverse synthesis operation | restriction | image | single root | single value | nature of synthesis operation)
带有注意力RPN和多关系检测器的小样本目标检测网络(提供源码和数据及下载)...
金仓数据库KingbaseES 插件kdb_date_function
Ffmpeg mix
跨境电商多商户系统怎么选
Why should programmers learn microservice architecture if they want to enter a large factory?
[Thesis Writing] how to write the overall design of JSP tourism network
Preliminary cognition of C language pointer
2022-02-14 (394. String decoding)
[dynamic programming] subsequence problem
[Chongqing Guangdong education] reference materials for design and a better life of Zhongyuan Institute of science and technology