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).
![](https://i.iter01.com/images/2a0342976676ba12caf799d60fc5eae2ef58ebe67b103303f47305c7cfb82414.jpg)
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).
![](https://i.iter01.com/images/2a0342976676ba12caf799d60fc5eae2ef58ebe67b103303f47305c7cfb82414.jpg)
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;
}
}
相關文章
- POJ3279 Fliptile【狀態壓縮+DFS】
- JavaScript 八進位制與二進位制表示法JavaScript
- JavaScript八進位制與二進位制表示法JavaScript
- 動態規劃——用二進位制表示集合的狀態壓縮DP動態規劃
- 二進位制與二進位制運算
- 進位制詳解:二進位制、八進位制和十六進位制
- JavaScript 二進位制、八進位制與十六進位制JavaScript
- 新型冠狀病毒轉二進位制(首發)
- 二進位制
- (二進位制)
- 十進位制——二 (八、十六 )進位制
- 二進位制,八進位制,十進位制,十六進位制的相互轉換
- 驗證二進位制數字正規表示式
- 【進位制轉換】二進位制、十六進位制、十進位制、八進位制對應關係
- 二進位制、十進位制與十六進位制相互轉化
- java中二進位制、八進位制、十進位制、十六進位制的轉換Java
- 二進位制,八進位制,十進位制,十六進位制之間的轉換
- Java中8進位制和16進位制的表示方法Java
- 計算機基礎進位制轉換(二進位制、八進位制、十進位制、十六進位制)計算機
- 二進位制轉十進位制快速方法
- JAVA 二進位制,八進位制,十六進位制,十進位制間進行相互轉換Java
- 什麼是二進位制?二進位制如何轉換?
- Cocoapods 二進位制
- 04 二進位制
- leetcode -- 二進位制LeetCode
- JavaScript十進位制轉換為二進位制JavaScript
- 十進位制轉二進位制推導(草稿)
- 對於十進位制數 -1023,包含符號位在內,至少需要多少個二進位制位表示該數符號
- [計算機基礎] 計算機進位制轉換:二進位制、八進位制、十進位制、十六進位制計算機
- 進位制之間的轉換之“十六進位制 轉 十進位制 轉 二進位制 方案”
- 一看就懂二進位制、八進位制、十六進位制數轉換十進位制
- 整數轉化成八進位制、十六進位制、二進位制,以及轉回
- 進位制與二進位制及相關轉換
- 二進位制陣列陣列
- 二進位制或序列
- 3416:【例72.1】 二進位制轉化為十進位制
- 遞迴函式實現十進位制正整數轉換為二進位制,八進位制,十六進位制遞迴函式
- java二進位制運算十進位制(精確運算)Java
- Python 十進位制轉換為二進位制 高位補零Python