zoukankan      html  css  js  c++  java
  • POJ--1699 Best Sequence(DP+dfs)

    Best Sequence
    Time Limit: 1000MS Memory Limit: 10000K
    Total Submissions: 5543 Accepted: 2188
    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
    之所以说是DP,是因为这道题目实现把每两个字符串连接的状态保存起来,而后进行dfs遍历,最后选出最优值

    #include <iostream>
    #include <string.h>
    #include <algorithm>
    #include <math.h>
    #include <stdlib.h>
    
    using namespace std;
    char a[11][25];
    int  dp[11][11];
    int n;
    int ans;
    int vis[15];
    void add(int m,int n)
    {
        int k=0;
        int len1=strlen(a[m]);
        int len2=strlen(a[n]);
        bool tag;
        for(int p=1;p<=len1&&p<=len2;p++)
        {
            tag=true;
            for(int i=0,j=len1-p;i<p;i++,j++)
            {
                if(a[m][j]!=a[n][i])
                {
                    tag=false;
                    break;
                }
            }
            if(tag)
                k=p;
        }
        dp[m][n]=len2-k;
    }
    void dfs(int pre,int num,int sum)
    {
    
        if(num==n)
        {
    
            if(ans>sum)
                ans=sum;
            return;
        }
        for(int i=0;i<n;i++)
        {
            if(vis[i]==0)
            {
                vis[i]=1;
                dfs(i,num+1,sum+dp[pre][i]);
                vis[i]=0;
            }
        }
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d",&n);
            getchar();
            for(int i=0;i<n;i++)
                scanf("%s",a[i]);
            memset(dp,0,sizeof(dp));
            for(int i=0;i<n;i++)
                for(int j=0;j<n;j++)
                    add(i,j);
            memset(vis,0,sizeof(vis));
            ans=9999;
            for(int i=0;i<n;i++)
            {
                vis[i]=1;
                dfs(i,1,strlen(a[i]));
                vis[i]=0;
            }
            printf("%d
    ",ans);
    
        }
        return 0;
    }
  • 相关阅读:
    前端自动化测试python+webdriver
    JavaScript 事件——“事件类型”中“HTML5事件”的注意要点
    onpageshow 监听页面是否是缓存页面
    CSS完美实现iframe高度自适应(支持跨域)
    HTML5 简单归纳 -- 前端知识 (一)
    JavaScrip 概述 -- 前端知识
    CSS 简单归纳 -- 前端知识
    HTML 简单归纳 -- 前端知识
    解决vscode-insider连接不了远程服务器问题
    回首,只为再出发!
  • 原文地址:https://www.cnblogs.com/dacc123/p/8228829.html
Copyright © 2011-2022 走看看