题目大意:给出一个字符串,计算与它按照字典序排序排列后的字符串有多少个位置不同。
解题思路:水体,sort一下,然后遍历一遍就好。
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; const int N = 105; char str[N], tmp[N]; int main () { int cas, n; scanf("%d", &cas); for (int i = 1; i <= cas; i++) { scanf("%d", &n); scanf("%s", str); strcpy(tmp, str); sort(tmp, tmp + n); int ans = 0; for (int j = 0; j < n; j++) if (str[j] != tmp[j]) ans++; printf("Case %d: %d ", i, ans); } return 0; }