Best Sequence
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 9988 | Accepted: 3835 |
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).
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
<span style="color:#000000">1
5
TCGG
GCAG
CCGC
GATC
ATCG
</span>
Sample Output
<span style="color:#000000">11</span>
Source
代码:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
//申明全局变量
int n, best, ** AddLen;//n条DNA片段,最优值,AddLen[i][j]第j片段后加入i片段的DNA
string* segs;//表示第i条DNA
bool* isVst;//表示第i条DNA已经加入片段
void DFS(int depth, int minLen, int rightSeg)//深度探索,剪枝
{
if (minLen > best)//剪枝
return;
if (depth >=n)//更新最小长度
{
best = minLen;
return;
}
for (int i = 0; i < n; i++)
{
if (!isVst[i])//当前基因片段没有加上去
{
isVst[i] = true;
DFS(depth + 1, minLen + AddLen[rightSeg][i], i);
isVst[i] = false;
}
}
}
void Process()//测试数据
{
fill(isVst, isVst + n, false);
best = 0;
for (int i = 0; i < n; i++)
best += segs[i].length();
for (int i = 0; i < n; i++)//遍历所有基因片段
for (int j = 0; j < n; j++)
{
if (i == j)
continue;
int minlen = segs[i].length() < segs[j].length() ? segs[i].length() : segs[j].length();//计算出两个字符串的最小长度
for (int k = minlen; k >= 0; k--) //检查匹配长度是否为k
{
int h = 0;
for (; h < k; h++)
{
if (segs[i][segs[i].length() - k + h] == segs[j][h])
continue;//有匹配的字符
else
break;//没有相匹配的字符
}
if (h == k)//最大匹配
{
AddLen[i][j] = segs[j].length() - k;
break;
}
}
}
for (int i = 0; i < n; i++)
{
isVst[i] = true;//将基因片段i加上去
DFS(1, segs[i].length(), i);//递归
isVst[i] = false; //将基因片段i撤消
}
}
int main()
{
int t;
cin >> t;
for (int i = 0; i < t; i++)
{
cin >> n;//设置n个基因规模
isVst = new bool[n];//初始化数组
segs = new string[n];
AddLen = new int* [n];
for (int i = 0; i < n; i++)
{
AddLen[i] = new int[n];
}
for (int i = 0; i < n; i++)//写入基因片段
{
cin >> segs[i];
}
Process();//处理基因片段
cout << best << '\n';//输出最优值
for (int i = 0; i < n; i++)//释放内存
{
delete[] AddLen[i];
}
delete[] AddLen;
delete[] segs;
delete[] isVst;
}
return 0;
}