Easy Comparison(ZCMU暑期提高六)

本文介绍了一种简单的字符串处理算法:首先对输入字符串进行字典序排序生成新字符串,然后逐字符对比原始字符串与排序后的字符串,最后输出两者不同的字符数量。示例代码使用 C++ 实现,展示了完整的输入输出流程。

Given a string S, your task is to simulate the following operations. At first, you should sort it by lexicographic order and generate an order string S’. Then, you should compare S’ with S character by character. Finally, you should output the number of different characters between S’ and S.

For example, if the original string S is “ACMICPC”, when we sort this string we could get a new string S’ “ACCCIMP”. Then we compare these two strings, we could find out that only two characters ‘A’ whose index is 0 and ‘C’ whose index is 1 are the same (index from 0), so there are 5 different characters.

Input

The first line of the input contains an integer T (T <= 10), indicating the number of cases. Each case begins with a line containing one integer n (1 <= n <= 100), the length of the string S. The next line contains the string, consisting of characters ‘A’ to ‘Z’.

Output

For each test case, print a line containing the test case number (beginning with 1) and the number of different characters between the two strings as said above.

Sample Input

4
2
AC
6
ACCEPT
7
ACMICPC
10
FZUACMICPC

Sample Output

Case 1: 0
Case 2: 0
Case 3: 5
Case 4: 10

 

字符串的简单操作,看懂题肯定过的题 看不懂题也能猜到题目意思(我就是)

A题速度太慢了,

#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
using namespace std;
int ca;
int main()
{
	ios::sync_with_stdio(false);
	int T;
	cin >> T;
	while (T--) {
		int n;
		string s;
		string t;
		s.clear();
		t.clear();
		cin >> n >> s;
		t = s;
		sort(s.begin(), s.end());
		int flag = 0;
		for (int i = 0;s[i];i++)  if (s[i] != t[i]) flag++;
		cout << "Case " << ++ca << ": " << flag << endl;
	}
	return 0;
}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值