Algorithm: UVA 11552

Source: Internet
Author: User

Meaning

Give a string and divide it into k blocks, such as the string "HelloWorld" into 2 chunks, "Hello", "World"

The letters inside each piece can be sorted in any order.

The final string, the consecutive same letter counts as a chunk, ask the total chunks at least how much?

Ideas

F[I][J]: The number J of Block I is ranked at the last of the least chunks

For each individual piece, its chunks is equal to the number of different letters that appear

Chunks of block I do chunks[i]

If the last of the I-1 block and the first bit of block I are the same, then you can combine these two, the total chunks can reduce a

F[I][J] = min{If the first bit of the K-bit of the i-1 block differs from that of the I block: f[i-1][k]+chunks [i],

If the i-1 block is the same as the first bit of block K and I: f[i-1][k]+chunks[i]-1}

Code

/**========================================== * is a solution for ACM/ICPC problem * * @source: uva-11552 Fewest Flops * @type: DP * @author: Shuangde * @blog: blog.csdn.net/shuangde800 * @email: zengshuangde@gmail.com *============== =============================*/#include <iostream> #include <cstdio> #include <algorithm> #include
    
<vector> #include <queue> #include <cmath> #include <cstring> using namespace std;
typedef long long Int64;
    
const int INF = 0X3F3F3F3F;
const int MAXN = 1010;
int k;
Char STR[MAXN];
int F[MAXN][MAXN];
    
BOOL vis[130];
    
int main () {int ncase;
    
scanf ("%d", &ncase);
    
while (ncase--) {scanf ("%d%s", &k, str);
    
    
int len = strlen (str);
    
Init memset (f, INF, sizeof (f)); for (int i = 0; i < len/k ++i) {//Calculate SI has several chunks//www.bianceng.cn int chunks = 0; memset (Vis, 0, sizeof (VIS
)); for (int j = i * k; J <= (i + 1) * K-1; ++j) {vis[str[j]] = TruE
    
for (int j = ' a '; J <= ' z '; ++j) if (vis[j]) ++chunks;
    
if (i = = 0) {for (int j = 0; j < K; ++j) f[i][j] = chunks; continue;} for (int j = 0; j < K; ++j) {int rear = i * k + j; for (int l = 0; l < K; ++l) {int pre = (i-1) * k + L; if (vis 
[Str[pre]] && (chunks = = 1 | | str[pre]!= str[rear])) {F[i][j] = min (f[i][j), F[i-1][l] + chunks-1);} else {
F[i][j] = min (F[i][j], f[i-1][l] + chunks);
an int ans = INF}}}
    
for (int i = 0; i < K ++i) ans = min (ans, f[len/k-1][i]);
printf ("%d\n", ans);
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.