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;
  }
}//字典序排序 


相关文章
|
2天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8077 2
|
11天前
|
域名解析 人工智能 Linux
|
19天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
10265 13
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
5天前
|
人工智能 安全 网络安全
|
10天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1547 5
|
9天前
|
云安全 数据采集 人工智能
|
16天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2697 8
|
9天前
|
Web App开发 缓存 安全
巨魔商店 TrollStore 安装教程(2026):免电脑在线安装与全机型安装器矩阵_iOS巨魔安装
巨魔商店(TrollStore)是iOS 14–17.0(特定版本)的永久签名工具,无需越狱,利用CoreTrust漏洞实现IPA签名长期有效,彻底告别7天重签。支持免电脑在线安装(TrollHelperOTA),适配多机型/系统组合,开源免费,安全可控。(239字)
2382 0