Description
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
解析
字符串转数字,然后排序再匹配
代码
#include<stdio.h>
#include<algorithm>
using namespace std;
#define MAX 200
int main()
{
int T,kase=1;
scanf("%d",&T);
while(T--)
{
char str[MAX];
int num[MAX],temp[MAX],n,i;
scanf("%d",&n);
scanf("%s",str);
for(i=0;i<n;i++)
{
num[i]=str[i]-'A';
temp[i]=num[i];
}
sort(num,num+n);
int ans=n;
for(i=0;i<n;i++)
{
if(num[i]==temp[i])
ans-=1;
}
printf("Case %d: %d\n",kase,ans);
kase+=1;
}
return 0;
}