zoukankan      html  css  js  c++  java
  • Best Sequence_DFS&&KMp

    Description

    The twenty-first century is a biology-technology developing century. One of the most attractive and challenging tasks is on the gene project, especially on gene sorting program. Recently we know that a gene is made of DNA. The nucleotide bases from which DNA is built are A(adenine), C(cytosine), G(guanine), and T(thymine). Given several segments of a gene, you are asked to make a shortest sequence from them. The sequence should use all the segments, and you cannot flip any of the segments. 

    For example, given 'TCGG', 'GCAG', 'CCGC', 'GATC' and 'ATCG', you can slide the segments in the following way and get a sequence of length 11. It is the shortest sequence (but may be not the only one). 

    Input

    The first line is an integer T (1 <= T <= 20), which shows the number of the cases. Then T test cases follow. The first line of every test case contains an integer N (1 <= N <= 10), which represents the number of segments. The following N lines express N segments, respectively. Assuming that the length of any segment is between 1 and 20.

    Output

    For each test case, print a line containing the length of the shortest sequence that can be made from these segments.

    Sample Input

    1
    5
    TCGG
    GCAG
    CCGC
    GATC
    ATCG
    

    Sample Output

    11

    【题意】找出最短的字符串长度,要求包含给出的所有字符串

    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    using namespace std;
    const int inf=0x7777777;
    int n,maxn;
    int mp[25][25],ln[25],vis[25],next1[25][25];
    char s[25][25];
    int kmp(int x,int y)
    {
        int i=0,j=0;
        while(i<ln[x])
        {
            if(s[x][i]==s[y][j])
            {
                i++;j++;
            }
            else
            {
                if(j==0) i++;
                else
                {
                    j=next1[x][j];
                }
            }
            if(j==ln[y]) return -1;//能在x中找到y;
        }
        return j;
    }
    void get_next(int k)
    {
        int j=-1;
        next1[k][0]=-1;
        for(int i=0;i<ln[k];i++)
        {
            if(j==-1||s[k][i]==s[k][j]) next1[k][++i]=++j;
            else j=next1[k][j];
        }
    }
    void dfs(int len,int cnt,int k)
    {
        if(cnt==n)//已经是第n个字符串了,获取最小的长度
        {
            if(len<maxn) maxn=len;
            return;
        }
        for(int i=1;i<=n;i++)
        {
            if(vis[i]==0)
            {
                vis[i]=1;
                if(mp[k][i]==-1)//如果字符串k和i包含关系
                {
                    dfs(len,cnt+1,k);//最后一个参数仍然是k
                }
                else dfs(len+ln[i]-mp[k][i],cnt+1,i);//如果不包含,两个串的长度相加减去相同部分长度
                vis[i]=0;
            }
        }
        return;
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d",&n);
            for(int i=1;i<=n;i++)
            {
                scanf("%s",s[i]);
                ln[i]=strlen(s[i]);
                get_next(i);//求next数组
            }
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                {
                    mp[i][j]=kmp(i,j);//求两个字符串的匹配度
                }
            }
            maxn=inf;
            memset(vis,0,sizeof(vis));
            for(int i=1;i<=n;i++)
            {
                vis[i]=1;
                dfs(ln[i],1,i);
                vis[i]=0;
            }
            printf("%d
    ",maxn);
        }
        return 0;
    }
  • 相关阅读:
    Cloudera Manager 4.6 安装部署hadoop CDH集群
    linux下统计目录下所有子目录的大小
    jvisualvm远程监控tomcat
    安装ubuntu server时可能会需要的配置
    安装配置maven私服-nexus
    maven环境配置
    各版本eclipse的maven配置
    转载:Centos7 从零编译Nginx+PHP+MySql 序言 一
    MongoDB系列一:CentOS7.2下安装mongoDB3.2.8
    MongoDB Windows环境安装及配置
  • 原文地址:https://www.cnblogs.com/iwantstrong/p/5871778.html
Copyright © 2011-2022 走看看