UVA 11552 fewest flops////two-dimensional linear dp////first, the block must be the same letter together, first record how many letters in each block//recorded as counts[i];////order F[i][j] Represents the minimum number of blocks with the letter J ending for the first I block////if the beginning letter of Block I is the same as the ending letter of block i-1//f[i][j] = min (F[i][j],f[i-1][k] + counts[i]-1);////otherwise////F[I][J] = Min (F[i][j],f[i-1][k] + counts[i])////The first case of the beginning and end of the same letter has a premise://F[i-1][k] K in block I appear after////condition is://1) block I only one character//2) The end character of block I is not the same as the end character of Block I-1////PS: The first case is the special of the second case, because each letter is either at the beginning,//is either at the end, does not have a continuous two block ending letter the same, this is certainly not//optimal, and if block I has only one character, So obviously it's not in this column. Wrong answer 10 times, so tenacious I am also drunk////keep practicing. # include <algorithm> #include <bitset> #include <cassert># Include <cctype> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric > #include <queue> #include <set> #include <stack> #include <vector>, #define CEIL (A, B) (((a) + (c)-1)/(b)) #define Endl ' \ n ' #define GCD __gcd#define highbit (x) (1ull<< (63-__builtin_ Clzll (x))) #define Popcount __builtin_popcountlltypedef Long long ll;using namespace Std;const int MOD = 1000000007;const L Ong Double PI = ACOs ( -1.L); const int MAXN = 1008;const int inf = 0x3f3f3f3f;char S[maxn];bool vis[maxn][30];int f[maxn][30 0];int N,k;int counts[maxn];void dp () {for (int i=0;i<=26;i++) {if (Vis[1][i]) {f[1][i] = counts[1];}} for (int i=2;i<=n/k;i++) {to (int j=0;j<26;j++) {if (Vis[i][j]) {for (int m=0;m<26;m++) {if (Vis[i][m] && (Counts[i]==1 | | j!=m)) {F[i][j] = min (f[i][j],f[i-1][m] + counts[i]-1);} Else{f[i][j] = min (f[i][j],f[i-1][m] + counts[i]);}}}} int ans = inf;for (int i=0;i<26;i++) {ans = min (ans,f[n/k][i]);} printf ("%d\n", ans);} void print () {for (int i=1;i<=n/k;i++) {cout << counts[i] << "";} cout << Endl;} void Init () {scanf ("%d%s", &k,s+1), n = strlen (s+1), memset (vis,0,sizeof (Vis)), memset (F,inf,sizeof (f)); memset (counts,0,sizeof (counts)); int x=1;for (int i=1;i<=n;i++) {if (!vis[x][s[i]-' a ']) {counts[x]++;vis[x][s[i] -' a '] = 1;} if (i%k==0) {x + +;}} cout << "x =" << x << endl;//print ();dp();} int main () {int T;//freopen ("E:\\code\\1.txt", "R", stdin), scanf ("%d", &t), while (t--) {init ();} return 0;}
UVA 11552 fewest Flops linear DP