首页 > 其他分享 >CodeForces CF1986C Update Queries题解

CodeForces CF1986C Update Queries题解

时间:2024-07-08 09:10:26浏览次数:11  
标签:set string 题解 Update 字符串 Queries ind input data

来吧,兄弟们,再来篇题解,其实也不是题解,是我自己的思路/心得/体会

Update Queries

题面翻译

题目翻译

一共 $t$ 组数据,每组数据给定长度为 $n$ 的字符串 $s$,长度为 $m$ 的 $ind$ 数组和字符串 $c$。

你可以任意安排 $ind$ 数组和字符串 $c$ 的顺序,并按照此顺序对字符串 $s$ 进行 $m$ 次操作:对于第 $i$ 次操作,将 $s_{ind_i}$ 的值更改为 $c_i$。要求经过 $m$ 次操作后的字符串 $s$ 的字典序最小,输出这个 $s$。

数据范围及约定

  • $1 \le n,m \le 10^5$;
  • 对于 $\forall i \in [1,m]$,有 $1 \le ind_i \le n$;
  • $1 \le t \le 10^4$;
  • $1 \le \sum n,\sum m \le 2 \times 10^5$;
  • 保证字符串 $s$ 和字符串 $c$ 仅包含小写字母。

题目描述

Let's consider the following simple problem. You are given a string $ s $ of length $ n $ , consisting of lowercase Latin letters, as well as an array of indices $ ind $ of length $ m $ ( $ 1 \leq ind_i \leq n $ ) and a string $ c $ of length $ m $ , consisting of lowercase Latin letters. Then, in order, you perform the update operations, namely, during the $ i $ -th operation, you set $ s_{ind_i} = c_i $ . Note that you perform all $ m $ operations from the first to the last.

Of course, if you change the order of indices in the array $ ind $ and/or the order of letters in the string $ c $ , you can get different results. Find the lexicographically smallest string $ s $ that can be obtained after $ m $ update operations, if you can rearrange the indices in the array $ ind $ and the letters in the string $ c $ as you like.

A string $ a $ is lexicographically less than a string $ b $ if and only if one of the following conditions is met:

  • $ a $ is a prefix of $ b $ , but $ a \neq b $ ;
  • in the first position where $ a $ and $ b $ differ, the symbol in string $ a $ is earlier in the alphabet than the corresponding symbol in string $ b $ .

输入格式

Each test consists of several sets of input data. The first line contains a single integer $ t $ ( $ 1 \leq t \leq 10^4 $ ) — the number of sets of input data. Then follows their description.

The first line of each set of input data contains two integers $ n $ and $ m $ ( $ 1 \leq n, m \leq 10^5 $ ) — the length of the string $ s $ and the number of updates.

The second line of each set of input data contains a string $ s $ of length $ n $ , consisting of lowercase Latin letters.

The third line of each set of input data contains $ m $ integers $ ind_1, ind_2, \ldots, ind_m $ ( $ 1 \leq ind_i \leq n $ ) — the array of indices $ ind $ .

The fourth line of each set of input data contains a string $ c $ of length $ m $ , consisting of lowercase Latin letters.

It is guaranteed that the sum of $ n $ over all sets of input data does not exceed $ 2 \cdot 10^5 $ . Similarly, the sum of $ m $ over all sets of input data does not exceed $ 2 \cdot 10^5 $ .

输出格式

For each set of input data, output the lexicographically smallest string $ s $ that can be obtained by rearranging the indices in the array $ ind $ and the letters in the string $ c $ as you like.

样例 #1

样例输入 #1

4
1 2
a
1 1
cb
4 4
meow
1 2 1 4
zcwz
7 4
abacaba
1 3 5 7
damn
7 10
traktor
7 6 5 4 3 2 1 6 4 2
codeforces

样例输出 #1

b
cwoz
abdcmbn
ccdeefo

提示

In the first set of input data, you can leave the array $ ind $ and the string $ c $ unchanged and simply perform all operations in that order.

In the second set of input data, you can set the array $ ind = [1, 1, 4, 2] $ and $ c = $ "zczw". Then the string $ s $ will change as follows: $ meow \rightarrow zeow \rightarrow ceow \rightarrow ceoz \rightarrow cwoz $ .

In the third set of input data, you can leave the array $ ind $ unchanged and set $ c = $ "admn". Then the string $ s $ will change as follows: $ abacaba \rightarrow abacaba \rightarrow abdcaba \rightarrow abdcmba \rightarrow abdcmbn $ .

我们解决这个题,首先要明白,什么是字典序。假设,从1到5,5个数。

最小情况就是1,2,3,4,5

最大就是5,4,3,2,1

序列相比是逐位对比的,从第一位开始,谁大谁的字典序就更大,如果一样就向后推,特殊的,类似这种,1比12的字典序要小。

结合输入分析题目,题目意思是给我们一个已知的序列s。我们要将ind数组结合字符串C按顺序修改字符串s并使其字典序最小。但我们可以任意将ind数组与c结合

4 4
meow
1 2 1 4
zcwz

原s数组是m e o w,c字符串是z c w z ,修改结果为c w o z。

如要让字符串的字典序最小,我们就需要让他按升序排列,在原序列的基础上对其进行修改,不仅是字符串需要升序,我们的修改也需要升序,这样的话,就可以最大限度的保证字典序最小。

特殊情况,例如这里面的1 1,有重复的,也就是说,这个值会被后来的值进行覆盖,我们可以利用这一点,如果有重复的修改点位,就将最大的(字典序)字母插入这里,后续被其他的所替代,代码上,我们可以直接删除重复的一步,并将最大字典序的字母删除。

所以该题的重点就是,去重以及排序.

代码如下#include

include<string.h>

include

include

using namespace std;
const int damn = 1e5 + 5;
char c[damn];
int num[damn];
char ind[damn];
int main()
{
int t;
cin >> t;
while (t--)
{
int n, m;
cin >> n >> m;
scanf("%s", c + 1);
for (int i = 1; i <= m; i++)
scanf("%d", &num[i]);
scanf("%s", ind + 1);
sort(ind + 1, ind + m + 1);
sort(num + 1, num + m + 1);
int cnt = 0;
for (int i = 1; i <= m; i++)
{
if (num[i] != num[i - 1])
{
c[num[i]] = ind[++cnt];
}
}
printf("%s\n", c + 1);
}
return 0;
}

标签:set,string,题解,Update,字符串,Queries,ind,input,data
From: https://www.cnblogs.com/lgdxxs12138/p/18289266

相关文章

  • **CodeForces CF1928B Equalize题解**
    ok兄弟们,今天本蒟蒻来做一篇小小的题解Equalize题面翻译有一个给定的长度为$n$的数列$a$,现在加上一个排列$b$,即$c_i=a_i+b_i$。现在求对于所有可能的$b$,$c$中出现最多的数的出现次数的最大值。translateby@UniGravity.题目描述Vasyahastwohobbies—addingpe......
  • qoj8225 最小值之和 题解
    题目链接点击打开链接题目解法很牛的题啊从\(f\)序列的最小值切入,考虑把\(f_i:=f_i-f_{min}\),会对\(f'\)造成什么影响?发现会使\(f'\)中的每个数都减去\((n-1)f_{min}\),且会把原问题分成\([1,min]\)和\([min+1,r]\)这两个完全相同的子问题于是考虑区间\(dp\),令......
  • 电脑开机检测不到硬盘怎么办 电脑检测不到硬盘问题解决
    电脑开机检测不到硬盘,无法进入系统或者显示“RebootandSelectproperBootdevice”等错误信息。这种情况可能会导致我们的数据丢失或者无法使用电脑。一、电脑检测不到硬盘的可能原因电脑检测不到硬盘的原因主要有以下几种:1、硬盘连接线松动或损坏:硬盘是通过SATA线或M.2插......
  • 启动应用程序出现wevtutil.exe找不到问题解决
    其实很多用户玩单机游戏或者安装软件的时候就出现过这种问题,如果是新手第一时间会认为是软件或游戏出错了,其实并不是这样,其主要原因就是你电脑系统的该dll文件丢失了或没有安装一些系统软件平台所需要的动态链接库,这时你可以下载这个wevtutil.exe文件(挑选合适的版本文件)把它......
  • 启动应用程序出现wininit.exe找不到问题解决
    其实很多用户玩单机游戏或者安装软件的时候就出现过这种问题,如果是新手第一时间会认为是软件或游戏出错了,其实并不是这样,其主要原因就是你电脑系统的该dll文件丢失了或没有安装一些系统软件平台所需要的动态链接库,这时你可以下载这个wininit.exe文件(挑选合适的版本文件)把它放......
  • [SNCPC2024] 2024 年陕西省大学生程序设计 J题猜质数II 题解
    题目链接:CF或者洛谷PS:CF的得等上gym。前提说明其实在上个月就见到这题了,当时很想做这题,结果找不到做题链接,也不知道出处,原来是陕西省赛的捧杯题。个人评价觉得是一道很不错的题,难度适中。讲解其实题解写的挺不错的,比很多比赛的题解写的详细许多了。这里站在我的角度分......
  • [题解]细胞自动机
    给定一个长度为\(n\)的\(01\)串\(s\),用于表示一个环上的细胞的初始状态,其中第\(1\)个细胞与第\(2\)个、第\(n\)个细胞相邻;第\(n\)个细胞与第\(1\)个和第\(n-1\)个相邻。\(0\)表示细胞死亡,\(1\)表示细胞存活。接下来给定\(t\)轮操作,每一轮操作,根据上一轮细胞的状态更改此轮的状态。......
  • P9668 [ICPC2022 Jinan R] Torch 题解
    思路考虑使用矩阵模拟这个过程。首先,我们可以设初值为:\[\begin{bmatrix}0&1\end{bmatrix}\]表示瘦子初始走\(0\)米,胖子初始走\(1\)米。考虑瘦子走一步。由于瘦子每走一步都不能超过胖子,我们可以使用\((\min,+)\)矩乘来维护这个性质。那么瘦子走一步是:\[\begin{bma......
  • CF292C Beautiful IP Addresses 题解(两种写法)
    题意一个IP地址是一个32位的2进制整数,分成四组8位的2进制整数(没有前导0)。比如说,0.255.1.123 是一个正确的IP地址,而0.256.1.123 和 0.255.1.01 不是正确的。定义一个合法的回文IP地址为BeautifulIPAddress(回文地址就是去掉“.”后是个回文字符串的地......
  • 沪越联赛 - 2024年6月月赛Div2 题解
    问题A:替换题目描述小明每次注释的时候都写\(n\)个/。小红看了小明的程序,觉得太难看了,那么多/,所以决定把这些没用的/都去掉。小红定义了一个宏命令,每次可以将所有连续的\(m\)个/替换成空(注意不是空格)小明得知了小红的企图后非常着急,因为他知道光把/都去掉,那么原......