CF1552A Subsequence Permutation(string排序大法)

简介: CF1552A Subsequence Permutation(string排序大法)

题目描述



A string ss of length nn , consisting of lowercase letters of the English alphabet, is given.


You must choose some number kk between 00 and nn . Then, you select kk characters of ss and permute them however you want. In this process, the positions of the other n-kn−k characters remain unchanged. You have to perform this operation exactly once.


For example, if s=\texttt{"andrea"}s="andrea" , you can choose the k=4k=4 characters \texttt{"a_d_ea"} and permute them into \texttt{"d_e_aa"} so that after the operation the string becomes \texttt{"dneraa"}"dneraa" .


Determine the minimum kk so that it is possible to sort ss alphabetically (that is, after the operation its characters appear in alphabetical order).


输入格式



The first line contains a single integer tt ( 1 \le t \le 10001≤t≤1000 ) — the number of test cases. Then tt test cases follow.


The first line of each test case contains one integer nn ( 1 \le n \le 401≤n≤40 ) — the length of the string.


The second line of each test case contains the string ss . It is guaranteed that ss contains only lowercase letters of the English alphabet.


输出格式



For each test case, output the minimum kk that allows you to obtain a string sorted alphabetically, through the operation described above.


题意翻译



题目大意:


有 t 组数据,每组数据会给你一个长度为 n 的字符串 s 。

你可以选择 k 个数,并把这 k 个数排序。使字符串 s 最后按照字典序排序。输出 k 的最小值。


输入输出样例



输入 #1复制

4

3

lol

10

codeforces

5

aaaaa

4

dcba


输出 #1复制

2

6

0

4


说明/提示



In the first test case, we can choose the k=2k=2 characters \texttt{"_ol"} and rearrange them as \texttt{"_lo"} (so the resulting string is \texttt{"llo"}"llo" ). It is not possible to sort the string choosing strictly less than 22 characters.


In the second test case, one possible way to sort ss is to consider the k=6k=6 characters \texttt{"_o__force_"} and rearrange them as \texttt{"_c__efoor_"} (so the resulting string is \texttt{"ccdeefoors"}"ccdeefoors" ). One can show that it is not possible to sort the string choosing strictly less than 66 characters.


In the third test case, string ss is already sorted (so we can choose k=0k=0 characters).


In the fourth test case, we can choose all k=4k=4 characters \texttt{"dcba"}"dcba" and reverse the whole string (so the resulting string is \texttt{"abcd"}"abcd" ).


题意,我们可以先把字符串拍好序,然后我们可以在进行比较字符串,找出几个不同。

#include<bits/stdc++.h> 
using namespace std;
int t,n;
string s1,s2;//string永远的神 
signed main(){
  cin>>t;
  while(t--){
    cin>>n>>s1;
    s2=s1;
    sort(s2.begin(),s2.begin()+s2.size());//先完整的排一遍序 
    int ans=0;
    for(int i=0;i<s2.size();i++)//然后找不同,看有几个 
      if(s1[i]!=s2[i]) ans++; 
    cout<<ans<<endl;
  }
}//字典序排序 


相关文章
|
Java
有关Java发送邮件信息(支持附件、html文件模板发送)
有关Java发送邮件信息(支持附件、html文件模板发送)
807 1
|
智能设计 UED
Dooring低代码关于辅助设计的思考和实践
Dooring低代码关于辅助设计的思考和实践
149 0
|
6月前
|
机器学习/深度学习 传感器 人工智能
《告别单一智能:神经符号混合系统驱动推理能力的跨界融合》
神经符号混合系统融合了神经网络与符号推理的优势,旨在赋予智能体大模型媲美人类的推理能力。神经网络擅长从数据中学习特征,但决策过程缺乏可解释性;符号推理基于规则和逻辑,具有高度可解释性,却难以应对复杂不确定性。神经符号混合系统通过结合两者,不仅提升了知识表示、推理决策和泛化能力,还在自动驾驶、金融预测、科学研究、智能教育和工业制造等领域展现出广泛应用前景。尽管当前仍面临模块融合与成本等挑战,但其发展潜力巨大,有望推动人工智能迈向更高水平,助力解决更多复杂现实问题。
268 10
《告别单一智能:神经符号混合系统驱动推理能力的跨界融合》
|
安全 Java 数据安全/隐私保护
密钥在手,安全无忧:探索Spring Boot中SecretKeySpec与Cipher的神秘力量
【8月更文挑战第29天】在现代软件开发中,数据安全至关重要。本文作为教程,介绍如何在Spring Boot应用中利用`SecretKeySpec`和`Cipher`实现基本的加密和解密功能。首先,需添加相关依赖,然后创建工具类封装加密与解密方法。通过示例演示了如何生成密钥、加密及解密数据。正确实现加密能有效保护敏感信息,增强应用安全性。开发者应根据需求选择合适算法和密钥长度,确保数据安全。
601 0
|
数据采集 前端开发 API
基于Qwen2大模型实现的中药智能化筛选助手
本文介绍了利用大语言模型微调技术在中药方剂智能化筛选与优化中的应用。项目涵盖微调环境搭建、数据预处理、智能体构建及效果评估等环节,展示了模型在生成新中药方剂上的创新能力和实用性。
基于Qwen2大模型实现的中药智能化筛选助手
|
前端开发
react动态生成input、select标签以及思路总结
本文介绍了在React中动态生成input和select标签的方法,包括准备数据结构、在组件挂载时动态添加状态、页面渲染以及输入处理,最后总结了实现思路。
203 1
react动态生成input、select标签以及思路总结
|
自然语言处理 数据可视化 Java
用Python手把手教你WordCloud可视化
用Python手把手教你WordCloud可视化
|
机器学习/深度学习 算法 安全
基于深度学习的目标检测的介绍(Introduction to object detection with deep learning)
基于深度学习的目标检测的介绍(Introduction to object detection with deep learning)
310 0
|
前端开发 JavaScript PHP
技术心得:前端点击按钮,导入excel文件,上传到后台,excel接收和更新数据
技术心得:前端点击按钮,导入excel文件,上传到后台,excel接收和更新数据
200 0
|
应用服务中间件 测试技术 Linux