Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

就是一道状压的水题,转移注意细节就好,比如没有包含情况。

Posted by lmc596922196 at 2015-08-17 11:32:38 on Problem 1699
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
const int INF = 0x3f3f3f3f;
#define B(x) (1<<(x))
char str[11][25];
int dp[B(10) + 5][11];
int nxt[25], add[11][11], n;

void get_next(char T[], int len){
    int i = 0, j = -1;
    nxt[i] = -1;
    while(i < len){
        if(j == -1 || T[i] == T[j]){
            i++;
            j++;
            nxt[i] = j;
        }else j = nxt[j];
    }
}

int kmp(char S[], char T[], int lenS, int lenT){
    int i = 0, j = 0;
    while(i < lenS){
        if(j == -1 || S[i] == T[j]){
            i++;
            j++;
        }else j = nxt[j];
    }
    return j;
}

void Init(){
    int len1, len2;
    for(int i = 0; i < n; i++){
        for(int j = i + 1; j < n; j++){
            len1 = strlen(str[i]);
            len2 = strlen(str[j]);
            get_next(str[j], len2);
            add[i][j] = len2 - kmp(str[i], str[j], len1, len2);
            get_next(str[i], len1);
            add[j][i] = len1 - kmp(str[j], str[i], len2, len1);
        }
    }
}

int main(){
    //freopen("read.txt", "r", stdin);
    int T;
    scanf("%d", &T);
    while(T--){
        scanf("%d", &n);
        for(int i = 0; i < n; i++){
            scanf("%s", str[i]);
        }
        Init();
        memset(dp, 0x3f, sizeof dp);
        int full = B(n) - 1;
        for(int i = 0; i < n; i++){
            dp[B(i)][i] = strlen(str[i]);
        }
        for(int s = 0; s <= full; s++){
            for(int i  = 0; i < n; i++){
                if(dp[s][i] == INF) continue;
                for(int j = 0; j < n; j++){
                    if((s & B(j))) continue;
                    dp[s | B(j)][j] = min(dp[s | B(j)][j], dp[s][i] + add[i][j]);
                }
            }
        }
        int ans = INF;
        for(int i = 0; i < n; i++){
            ans = min(ans, dp[full][i]);
        }
        printf("%d\n", ans);
    }
    return 0;
}




Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator