(2817)POJ-状态压缩

#include<iostream>
#include<cstdio>
#include<string.h>
#include<string>
#include<stack>
#include<set>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<set>


#define ll __int64
#define lll unsigned long long
#define llf long double
#define db double
#define MAX 600
#define eps 1e-8
#define mod 100000000


using namespace std;


/*

题意:给你n个字符串,你在任意排列下,问i和i+1字符串的公共连续字符长度之和最大,这些排列的字符前面可以有任意的空格。

解法:http://www.cnblogs.com/PureMilk/archive/2008/07/17/1245085.html#2163586  :) 贴个吧,按照这里写的

*/

int op[509][509];//字符串i和字符串j的最长公共字符个数
int dp[2000][20];//
int n;


void LCS(string a,string b,int aa,int bb)
{
    int _max = 0;
    for(int i = 0; i<a.size(); i++)
    {
        int ans = 0;
        for(int j = 0; j<b.size(); j++)
        {
            if(i+j<a.size()&&b[j]==a[i+j])
            {
                ans++;
            }
        }
        _max = max(_max,ans);
    }
    for(int i = 0; i<b.size(); i++)
    {
        int ans = 0;
        for(int j = 0; j<a.size(); j++)
        {
            if(i+j<b.size()&&a[j]==b[i+j])
            {
                ans++;
            }
        }
        _max = max(_max,ans);
    }
    op[aa][bb] = op[bb][aa] = _max;
}


int ok(int staus,int last)
{
    int maxx = 0;
    int new_staus = staus;
    new_staus&=(~(1<<(last-1)));


    if(new_staus == 0) return 0;
    if(dp[new_staus][last]) return dp[new_staus][last];


    for(int i = 0; i<n; i++)
    {
        int sum = 0;
        int tmp = new_staus&(~(1<<i));
        if(tmp!=new_staus) sum = ok(tmp,i+1) + op[i+1][last];
        maxx = max(maxx,sum);
    }
    dp[staus][last] = maxx;
    return maxx;
}


string s[MAX];
int a[MAX];


int main()
{
    int _max;
    while(~scanf("%d",&n)&&n)
    {
        for(int i = 1; i<=n; i++)
        {
            cin>>s[i];
        }
        for(int i = 1; i<=n; i++)
        {
            for(int j = i+1; j<=n; j++)
            {
                LCS(s[i],s[j],i,j);
            }
        }
        memset(dp,0,sizeof(dp));
        _max = 0;
        for(int i = 1; i<=n; i++)
        {
            //cout<<ok((1<<n)-1,i)<<endl;
            _max = max(_max,ok((1<<n)-1,i));
        }
        printf("%d\n",_max);
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值