F - Substring of Sorted String
题目链接
题解(树状数组)
我们维护两个树状数组,一个记录 \(1\sim i\) 中 \(s_i>s_{i+1}\)的数量,即逆序对数量,另一个记录 \(1\sim i\) 中 \(26\) 个字母的数量。
在修改时很好维护,先把原来的字母数量减 \(1\) ,再新的字母数加 \(1\) ,逆序对以类似方式维护。
重点在查询,一个区间 \([l,r]\) 被包含在排序后的字符串中,当且仅当这个区间里没有逆序对(即统计逆序对数量的树状数组 \([l,r-1]\) ,注意,右端点是 \(r-1\) ,如果右端点是 \(r\) 会统计到 \(s_r > s_{r+1}\) ,不符合我们要求),且除了最头上的字母外中间的字母(大于第一个字母小于最后一个字母)应和所有的字符数量相同(要包含排好序的字符串中连续字符一定要选上,如果 \([l,r]\) 区间的字符串包含了大于第一个字母小于最后一个字母的字母,那么中间的所有字母应包含整个字符串中的字母,也就是说,这个子串中中间的字母应和整个字符串中的字母书相同)。
代码
#include <iostream>
using namespace std;
const int N = 100010;
int n,q;
string s;
int c[N];
int cnt[26][N];
int lowbit (int x) {
return x & -x;
}
void modify (int c[],int x,int d) {
for (int i = x;i <= n;i += lowbit (i)) c[i] += d;
}
int query (int c[],int x) {
int ans = 0;
for (int i = x;i;i -= lowbit (i)) ans += c[i];
return ans;
}
int main () {
cin >> n >> s >> q;
s = ' ' + s;
for (int i = 1;i <= n;i++) modify (cnt[s[i] - 'a'],i,1);
for (int i = 1;i < n;i++) {
if (s[i] > s[i + 1]) modify (c,i,1);
}
while (q--) {
int op;
cin >> op;
if (op == 1) {
int x;
char ch;
cin >> x >> ch;
// if (x + 1 <= n && s[x] > s[x + 1] && ch <= s[x + 1]) modify (c,x,-1);
// else if (x + 1 <= n && s[x] <= s[x + 1] && ch > s[x + 1]) modify (c,x,1);
// if (x - 1 >= 1 && s[x - 1] > s[x] && ch >= s[x - 1]) modify (c,x - 1,-1);
// else if (x - 1 >= 1 && s[x - 1] <= s[x] && ch < s[x - 1]) modify (c,x - 1,1);
modify (cnt[s[x] - 'a'],x,-1),modify (cnt[ch - 'a'],x,1);
if (x - 1 >= 1 && s[x - 1] > s[x]) modify (c,x - 1,-1);
if (x + 1 <= n && s[x] > s[x + 1]) modify (c,x,-1);
s[x] = ch;
if (x - 1 >= 1 && s[x - 1] > s[x]) modify (c,x - 1,1);
if (x + 1 <= n && s[x] > s[x + 1]) modify (c,x,1);
}
else {
int l,r;
cin >> l >> r;
bool ans = true;
for (int i = s[l] - 'a' + 1;i <= s[r] - 'a' - 1;i++) ans &= query (cnt[i],r) - query (cnt[i],l - 1) == query (cnt[i],n);
if (ans && query (c,r - 1) - query (c,l - 1) == 0) puts ("Yes");
else puts ("No");
}
}
return 0;
}
F - Substring of Sorted String的更多相关文章
- 【CodeForces】914 F. Substrings in a String bitset
[题目]F. Substrings in a String [题意]给定小写字母字符串s,支持两种操作:1.修改某个位置的字符,2.给定字符串y,查询区间[l,r]内出现y多少次.|s|,Σ|y|&l ...
- ACM-ICPC 2019南昌网络赛F题 Megumi With String
ACM-ICPC 南昌网络赛F题 Megumi With String 题目描述 给一个长度为\(l\)的字符串\(S\),和关于\(x\)的\(k\)次多项式\(G[x]\).当一个字符串\(str ...
- java用substring函数截取string中一段字符串
在String中有两个substring()函数,如下: 一:String.substring(int start) 参数: start:要截取位置的索引 返回: 从start开始到结束的字符串 例如 ...
- LeetCode第[5]题(Java):Longest Palindromic Substring 标签:String、动态规划
题目中文:求最长回文子串 题目难度:Medium 题目内容: Given a string s, find the longest palindromic substring in s. You ma ...
- LeetCode 1234. Replace the Substring for Balanced String
原题链接在这里:https://leetcode.com/problems/replace-the-substring-for-balanced-string/ 题目: You are given a ...
- 【leetcode】1234. Replace the Substring for Balanced String
题目如下: You are given a string containing only 4 kinds of characters 'Q', 'W', 'E' and 'R'. A string i ...
- 2016多校联合训练4 F - Substring 后缀数组
Description ?? is practicing his program skill, and now he is given a string, he has to calculate th ...
- Codecraft-18 and Codeforces Round #458 (Div. 1 + Div. 2, combined) F. Substrings in a String
http://codeforces.com/contest/914/problem/F 以前做过一个类似的,但是查询的子串长度最多是10,这个时候就是用bit + hash做了.这是因为改变一个字符, ...
- check sub-string in the string
if "blah" not in somestring: continue
- AtCoder Beginner Contest 285 解题报告
AtCoder Beginner Contest 285 解题报告 \(\text{DaiRuiChen007}\) Contest Link A. Edge Checker 2 假设 \(a\ge ...
随机推荐
- rdlc 中文在win10中显示异常,在非win10中显示正常
RDLC中的默认字体Arial. 在win10中,RDLC为Arial时,显示中文会异常,这个时候,只需要将Font修改为中文字体就可以了,例如修改为宋体.
- 040_Field Set
Field Set是一组字段.例如,您可以有一个Field Set,其中包含描述用户的名字,中间名,姓氏和业务标题的字段.将Field Set添加到Visualforce页面时,开发人员可以遍历字段并 ...
- JS篇(005)-== 和 === 的不同
答案:==是抽象相等运算符,而===是严格相等运算符.==运算符是在进行必要的类型转换后,再比较.===运算符不会进行类型转换,所以如果两个值不是相同的类型,会直接返回false.使用==时,可能发生 ...
- React使用portal提示 The types returned by 'render()' are incompatible between these types. Type 'ReactPortal' is not assignable to type 'ReactNode'. Type '{}' is not assignable to type 'ReactNode'.
原因:组件返回了个不是<></>的东西 原先代码: export default class Index extends React.PureComponent { rende ...
- holiday12
holiday12--linux basis super user(root) In linux, account root usually use for system maintain and m ...
- mybatis-generator 生成实体类,表字段类型有text,longtext,生成**WithBLOBs解决办法
modeType="flat" 其他参数请查询文档http://www.youbiji.cn/doc/mybatis/xml-table.html
- Study python_02
分支结构 简单的使用if语句 使用if-else import random# 调用一个随机数包(只看if的情况可忽略) n1 = random.randrange(100) n2 = random. ...
- flutter 顶部导航tabbar自定义
本文使用tabbar实现顶部横向滚动多个菜单. 实现tabbar搜索框功能加功能按钮. 话不多说,上代码! import 'package:flutter/cupertino.dart'; impor ...
- 浏览器F12,Network中各按钮的作用
Network下 preserve log:勾选,页面发生跳转,接口不丢失:(比如登录成功跳转到首页,登录的接口就没了,勾选Perserve log,会记录跳转前的接口): Disable cache ...
- react文件分片上传
参考文档: https://blog.csdn.net/weixin_39887846/article/details/113492372 https://juejin.cn/post/6844904 ...