标签:
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 6 const int maxn = 1000 + 10; 7 const int maxm = 30; 8 9 int n, k; 10 11 char s[maxn]; 12 int d[maxn][maxm]; 13 14 int cnt[maxn]; 15 bool vis[maxn][maxm]; 16 17 int main() 18 { 19 int T; scanf("%d", &T); 20 while(T--) 21 { 22 scanf("%d %s", &k, s); 23 n = strlen(s); 24 25 memset(d, 0x3f, sizeof(d)); 26 memset(cnt, 0, sizeof(cnt)); 27 memset(vis, false, sizeof(vis)); 28 for(int i = 0; i < n; i += k) 29 { 30 for(int j = 0; j < k; j++) 31 { 32 int ch = s[i + j] - ‘a‘; 33 if(!vis[i][ch]) cnt[i]++; 34 vis[i][ch] = true; 35 } 36 } 37 38 for(int i = 0; i < 26; i++) if(vis[0][i]) d[0][i] = cnt[0]; 39 40 for(int i = k; i < n; i += k) 41 for(int j = 0; j < 26; j++) if(vis[i][j]) 42 for(int t = 0; t < 26; t++) if(vis[i - k][t]) 43 { 44 if(vis[i][t] && (j != t || cnt[i] == 1)) 45 d[i][j] = min(d[i][j], d[i-k][t] + cnt[i] - 1); 46 else 47 d[i][j] = min(d[i][j], d[i-k][t] + cnt[i]); 48 } 49 50 int ans = n; 51 for(int i = 0; i < 26; i++) ans = min(ans, d[n - k][i]); 52 printf("%d\n", ans); 53 } 54 55 return 0; 56 }
标签:
原文地址:http://www.cnblogs.com/AOQNRMGYXLMV/p/4700502.html