###### UVA 11552 Fewest Flops

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <utility>
using namespace std;

#define rep(i,j,k) for (int i=j;i<=k;i++)
#define Rrep(i,j,k) for (int i=j;i>=k;i--)

#define Clean(x,y) memset(x,y,sizeof(x))
#define LL long long
#define ULL unsigned long long
#define inf 0x7fffffff
#define mod 100000007
const int maxn = 1009;

int T,k,n;
char s[maxn];
int kind[maxn]; //每个块内的字符种类
vector<int> p[maxn];  //块内字符
bool g[maxn][27]; //第i个块是否含有j
int dp[maxn][27];

void init()
{
bool flag[30];
int len = strlen(s+1);
n = len / k;
int pos = 0;
Clean(g,false);
rep(i,1,n)
{
kind[i] = 0;
p[i].clear();
p[i].push_back(0);
Clean(flag,true);
rep(j,1,k)
{
int x = s[++pos] - 'a' + 1;
if ( flag[x] ) kind[i]++ , flag[x] = false , p[i].push_back(x);
g[i][x] = true;
}
}
}

void solve()
{
Clean(dp,0x3f);
rep(i,1,kind[1])
dp[1][i] = kind[1];

rep(i,2,n)
{
rep(j,1,kind[i])
rep(k,1,kind[i-1])
{
if ( ( g[i][p[i-1][k]] && p[i][j] != p[i-1][k] ) || ( kind[i] == 1 && p[i][j] == p[i-1][k] ) ) //有相同的字符，放在一起
dp[i][j] = min( dp[i][j] , dp[i-1][k] + kind[i] - 1 );
else
dp[i][j] = min( dp[i][j] , dp[i-1][k] + kind[i] );
}
}
int ans = inf;
rep(i,1,kind[n])
ans = min( ans , dp[n][i] );
cout<<ans<<endl;
}

int main()
{
cin>>T;
while(T--)
{
scanf("%d %s",&k,s+1);
init();
solve();
}
return 0;
}

#### UVA 11552-Fewest Flops（DP）

2015-03-07 00:04:38

#### UVA 11552 Fewest Flops

2013-09-01 14:06:37

#### UVa:11552 - Fewest Flops

2014-03-04 22:54:13

#### UVA 11552——Fewest Flops

2016-03-28 21:29:15

#### UVA 11552 Fewest Flops .

2016-11-17 21:32:05

#### UVA - 11552 Fewest Flops

2013-11-16 14:03:55

#### UVA 11552 Fewest Flops

2015-11-24 11:56:04

#### Uva 11552 Fewest Flops

2016-08-15 21:19:18

#### UVA - 11552 Fewest Flops (dp)

2017-05-16 16:26:12

## 不良信息举报

UVA 11552 Fewest Flops