C
Accept:78 |
|
Time |
|
Description
我们将仅由若干个同一小写字母构成的字符串称之为简单串,例如"aaaa"是一个简单串,而"abcd"则不是简单串。现在给你一个仅由小写字母组成的字符串,你需要用最小的花费,将其转换成一个简单串。
InputFormat
第一行一个整数T(T≤100),表示测试数据的组数。
OutputFormat
对于每一组数据,输出一个整数,表示将字符串转换成简单串的最小花费。
SampleInput
2
abbba
abbac
SampleOutput
2
3
# include <stdio.h> # include <string.h> int main() { int t, l, temp; int i, j, k; char str[1002], ch[26]; int n[26]; scanf("%d", &t); ch[0] = 'a'; for (i = 0; i < 26; i++) ch[i + 1] = ch[i] + 1; for (k = 0; k < t; k++) { scanf("%s", str); memset(n, 0, sizeof(n)); l = strlen(str); for (j = 0; j < 26; j++) { for (i = 0; str[i] != '\0'; i++) { if (str[i] == ch[j]) n[j]++; } } for (i = 0; i < 26; i++) for (j = 0; j < 26 - i; j++) if (n[j + 1] < n[j]) { temp = n[j + 1]; n[j + 1] = n[j]; n[j] = temp; } printf("%d\n", l - n[25]); } return 0; }