Time Limit:2000MS | Memory Limit:Unknown | 64bit IO Format:%lld & %llu |
Description

Problem F
FEWEST FLOPS
A common way to uniquely encode a string is by replacing its consecutive repeating characters (or “chunks”) by the number of times the character occurs followed by the character itself. For example, the string “aabbbaabaaaa” may be encoded as “2a3b2a1b4a”. (Note for this problem even a single character “b” is replaced by “1b”.)
Suppose we have a string S and a number k such that k divides the length of S. Let S1 be the substring of S from 1 to k, S2 be the substring of S from k + 1 to 2k, and so on. We wish to rearrange the characters of each block Si independently so that the concatenation of those permutations S’ has as few chunks of the same character as possible. Output the fewest number of chunks.
For example, let S be “uuvuwwuv” and k be 4. Then S1 is “uuvu” and has three chunks, but may be rearranged to “uuuv” which has two chunks. Similarly, S2 may be rearranged to “vuww”. Then S’, or S1S2, is “uuuvvuww” which is 4 chunks, indeed the minimum number of chunks.
Program Input
The input begins with a line containing t (1 ≤ t ≤ 100), the number of test cases. The following t lines contain an integer k and a string S made of no more than 1000 lowercase English alphabet letters. It is guaranteed that k will divide the length of S.
Program Output
For each test case, output a single line containing the minimum number of chunks after we rearrange S as described above.
INPUT
2 5 helloworld 7 thefewestflops
OUTPUT
8 10
Calgary Collegiate Programming Contest 2008
题意:
输入正整数k和字符串S,字符串长度为k的倍数,把S按照从左到右的顺序每k个分为一组,每组之间可以任意重排,组与组之间先后顺序保持不变,你的任务是让重排后的字符串包含尽量少的块,其中每个块为连续的相同字母。
思路:
简单DP
前i-1组影响第i组的值的因素为第i-1组的最后一个字符
dp[i][j] 表示前i组重排并且以j为结尾的最少块数
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>
using namespace std;
//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif
#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define P64I1(a) printf(oform1, (a))
#define REP(i, n) for(int (i)=0; (i)<n; (i)++)
#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)
const int INF = 0x3f3f3f3f;
const double eps = 10e-9;
const double PI = (4.0*atan(1.0));
const int maxn = 1000 + 20;
char S[maxn];
int dp[maxn][30];
int vis[30];
int tvis[30];
int main() {
int T;
scanf("%d", &T);
while(T--) {
int K;
scanf("%d%s", &K, S);
int n = strlen(S) / K;
memset(vis, 0, sizeof(vis));
memset(dp, 0x3f, sizeof(dp));
for(int i=0; i<K; i++) vis[S[i]-'a'] = 1;
int cnt = 0;
for(int i=0; i<26; i++) if(vis[i]) cnt++;
for(int i=0; i<26; i++) {
dp[0][i] = vis[i] ? cnt : INF;
}
for(int i=1; i<n; i++) {
memset(vis, 0, sizeof(vis));
memset(tvis, 0, sizeof(tvis));
for(int j=K*i; j<K*(i+1); j++) vis[S[j]-'a'] = 1;
for(int j=K*(i-1); j<K*i; j++) tvis[S[j]-'a'] = 1;
cnt = 0;
for(int j=0; j<26; j++) if(vis[j]) cnt++;
if(cnt == 1) {
int sc = S[K*i] - 'a';
for(int j=0; j<=26; j++) {
int t = dp[i-1][j] + cnt;
if(j == sc) t--;
dp[i][sc] = min(dp[i][sc], t);
}
continue;
}
for(int j=0; j<26; j++) if(vis[j]) {
for(int p=0; p<26; p++) if(vis[p] && j != p) {
for(int q=0; q<26; q++) if(tvis[q]) {
int t = dp[i-1][q] + cnt;
if(p == q) t--;
dp[i][j] = min(dp[i][j], t);
}
}
}
}
int ans = INF;
for(int i='a'; i<='z'; i++) ans = min(ans, dp[n-1][i-'a']);
printf("%d\n", ans);
}
return 0;
}