POJ 1699 二进制表示状态+dfs

好长时间没有做计划了啊,这道题目还是放假之前看的,一放假乱七八糟的啊,没有dbug,今天看了一下,手残了一点啊。

题意很好理解:就是给你n串字母看怎么才能做成最小的一串字符。期间用到了二进制表示剩余的所有的子状态:http://blog.csdn.net/fulongxu/article/details/22953717。然后再dfs枚举就可以了啊。注意如果不能合并的就不用再算了啊。

Best Sequence
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 4707 Accepted: 1865

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 <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-7
#define M 1000100
#define LL __int64
//#define LL long long
#define INF 0x3fffffff
#define PI 3.1415926535898

using namespace std;

const int maxn = 30;

char str[maxn][maxn];
int dis[maxn][maxn];
int vis[maxn];
int f[2010];
int cnt;
int Min;

int judge(char s1[], char s2[])
{
    int len1 = strlen(s1);
    int len2 = strlen(s2);
    int ans = 0;
    int i, j;
    for(i = 0; i < len1; i++)
    {
        if(s1[i] == s2[0])
        {
            for(j = 1; j < len2 && i+j < len1; j++)
            {
                if(s1[i+j] != s2[j])
                    break;
            }
            if(j == len2 || i+j == len1)
                break;
        }
    }
    ans = len2-(len1-i);
    return ans;
}


void dfs(int x, int len, int s)
{
    s = s-(1<<x);
    int flag = 0;
    int ts = s;
    while(s)
    {
        int tt = (s-1)&s;
        int ss = s-tt;
        int xx = f[ss];
        s = s - (1<<xx);
        dfs(xx, len+dis[x][xx], ts);
        flag = 1;
    }
    if(!flag)
        Min = min(Min, len);
}


int main()
{
    for(int i = 0; i <= 10; i++)
        f[(1<<i)] = i;

    for(int i = 1; i <= (1<<10); i++)
        if(!f[i]) f[i] = f[i-1];
    int T;
    cin >>T;
    while(T--)
    {
        int n;
        cin >>n;
        for(int i = 0; i < n; i++)
            cin >>str[i];
        Min = INF;
        memset(vis, 0 , sizeof(vis));
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < n; j++)
            {
                if(i == j || vis[i] || vis[j])
                {
                    continue;
                }
                else
                {
                    int ans = judge(str[i], str[j]);
                    dis[i][j] = ans;
                    if(ans <= 0)
                        vis[j] = 1;
                }
            }
        }

        int ss = (1<<n)-1;
        for(int i = 0; i < n; i++)
            if(vis[i]) ss -= (1<<i);
        for(int i = 0; i < n; i++)
            if(!vis[i]) dfs(i, strlen(str[i]), ss);
        cout<<Min<<endl;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值