POJ--1699 -- Best Sequence [extended KMP + DFS], poj -- 1699 -- bestkmp

Source: Internet
Author: User

POJ--1699 -- Best Sequence [extended KMP + DFS], poj -- 1699 -- bestkmp

Link:Http://poj.org/problem? Id = 1699

Question:Give n strings and find the minimum length of their connection. If the first and last letters are the same, they can share the same part. For example, if two strings ABCDEF and DEFGHI are connected to ABCDEFGHI, the minimum length is 9, the DEF part in the middle is shared.


Ideas:Because the data size is small, we first use the extended KMP to obtain the length that a can share after connecting to B for every two strings a and B, use array B [I] [j] to indicate the maximum length that can be shared after the string I.

1. in the extended KMP function, obtain the public prefix array ret [I] (sub-string, parent string, and public prefix array) of substrings a and B, if ret [I] is equal to the value of strlen (B)-I at this time, it indicates the rest part of string B and the front strlen (B) of string) -If the I characters are completely equal, the value is the value we need. Because I is small to large, if we find the matching strlen (B)-I value, the value that appears first must be the largest and can be returned. If no matching value is found at the end, it indicates that no Suffix of string B is equal to the prefix of string a, and 0 is returned.

2. after finding all the values in array B, consider that a string can only be connected to one character string at most, and only one character string can be connected to one character string at most, and the data of this question is small, you can use dfs to enumerate the first string of each string (that is, it is not connected to another string), and then dfs to enumerate the Next string of each string, finding the minimum value is the answer we need.


#include<cstring>#include<string>#include<fstream>#include<iostream>#include<iomanip>#include<cstdio>#include<cctype>#include<algorithm>#include<queue>#include<map>#include<set>#include<vector>#include<stack>#include<ctime>#include<cstdlib>#include<functional>#include<cmath>using namespace std;#define PI acos(-1.0)#define MAXN 10100#define eps 1e-7#define INF 0x7FFFFFFF#define LLINF 0x7FFFFFFFFFFFFFFF#define seed 131#define MOD 1000000007#define ll long long#define ull unsigned ll#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1char a[15][25];int next[MAXN],ret[MAXN];int b[15][15],vis[15];int ans,n;int ExtendKMP(char a[],char b[]){    int i,j,k;    int M = strlen(a);    int N = strlen(b);    for(j=0;1+j<M&&a[j]==a[1+j];j++);    next[1] = j;    k = 1;    for(i=2;i<M;i++){        int Len = k + next[k], L = next[i-k];        if(L<Len-i){            next[i] = L;        }        else{            for(j=max(0,Len-i);i+j<M&&a[j]==a[i+j];j++);            next[i] = j;            k = i;        }    }    for(j=0;j<N&&j<M&&a[j]==b[j];j++);    if(j==N)    return N;    ret[0] = j;    k = 0;    for(i=1;i<N;i++){        int Len = k + ret[k], L = next[i-k];        if(L<Len-i){            ret[i] = L;        }        else{            for(j=max(0,Len-i);j<M&&i+j<N&&a[j]==b[i+j];j++);            ret[i] = j;            k = i;        }        if(ret[i]==N-i) return ret[i];    }    return 0;}void dfs(int x,int tot,int len){    if(tot==n){        if(ans>len) ans = len;        return ;    }    if(len>ans) return ;    for(int i=0;i<n;i++){        if(!vis[i]){            vis[i] = 1;            dfs(i,tot+1,len+strlen(a[i])-b[i][x]);            vis[i] = 0;        }    }}int main(){    int t,i,j;    scanf("%d",&t);    while(t--){        memset(b,0,sizeof(b));        memset(vis,0,sizeof(vis));        ans = INF;        scanf("%d",&n);        for(i=0;i<n;i++){            scanf("%s",a[i]);        }        for(i=0;i<n;i++){            for(j=0;j<n;j++){                if(i==j)    continue;                b[j][i] = ExtendKMP(a[i],a[j]);            }        }        for(i=0;i<n;i++){            vis[i] = 1;            dfs(i,1,strlen(a[i]));            vis[i] = 0;        }        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.