zoukankan      html  css  js  c++  java
  • UVa 11552 Fewest Flops (DP)

    题意:给一个字符串,把它分为k块,每一块里面的字母可以任意的排序。最终字符串, 连续的一样的字母算作一个chunk,问总chunks最少是多少?

    析:dp[i][j] 表示第 i 个块,第 j 位在末尾时chunk最少,状态转移方程也应该好写,如果 dp[i-1][j] 和第 i 块第一个一样,那么就总数就会-1,

    否则就是直接加上。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <sstream>
    #include <unordered_map>
    #include <unordered_set>
    #define debug() puts("++++");
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const LL LNF = 1LL << 60;
    const double inf = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 1000 + 5;
    const int mod = 2000;
    const int dr[] = {-1, 1, 0, 0};
    const int dc[] = {0, 0, 1, -1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c){
        return r >= 0 && r < n && c >= 0 && c < m;
    }
    int dp[maxn][maxn];
    char s[maxn];
    int a[maxn];
    bool vis[30];
    
    int main(){
        int T;  cin >> T;
        while(T--){
            scanf("%d", &n);
            int cnt = 0;
            scanf("%s", s);
            int len = strlen(s);
            int t = len / n;
            memset(dp, INF, sizeof dp);
            memset(vis, false, sizeof vis);
            for(int i = 0; i < n; ++i)
                vis[s[i]-'a'] = true;
            for(int i = 0; i < 26; ++i)  if(vis[i])  ++cnt;
            for(int i = 0; i < n; ++i)  dp[0][i] = cnt;
    
            for(int i = 1; i < t; ++i){
                cnt = 0;
                memset(vis, false, sizeof vis);
                for(int j = i*n; j < (i+1)*n; ++j)
                    vis[s[j]-'a'] = true;
                for(int j = 0; j < 26; ++j)  if(vis[j])  ++cnt;
    
                for(int j = 0; j < n; ++j){
                    int fro = i * n + j;
                    for(int k = 0; k < n; ++k){
                        int rear = (i-1) * n + k;
                        if(vis[s[rear]-'a'] && (1 == cnt || s[rear] != s[fro]))  dp[i][j] = min(dp[i][j], dp[i-1][k] + cnt - 1);
                        else   dp[i][j] = min(dp[i][j], dp[i-1][k] + cnt);
                    }
                }
            }
            int ans = INF;
            for(int i = 0; i < n; ++i)  ans = min(ans, dp[t-1][i]);
            printf("%d
    ", ans);
        }
        return 0;
    }
    
  • 相关阅读:
    6.html5分组元素
    STL基础--算法(修改数据的算法)
    STL基础--算法(不修改数据的算法)
    STL基础--仿函数(函数对象)
    STL基础--迭代器和算法
    STL基础--容器
    STL基础--基本介绍
    C++11--Tuple类<tuple>
    C++11--随机数引擎和随机数分布<random>
    C++11--时钟和计时器<chrono>
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/6559373.html
Copyright © 2011-2022 走看看