DNA sequence HDU - 1560(IDA*)
作者:互联网
题目链接
题意:
题目分析:
首先注意到n的范围很小,可以得知答案序列并不会很长,直接深搜我们需要记录每一个序列正在匹配的位置,可以使用IDA* 来优化,启发函数就是当前未匹配的序列中的最大长度,这里我们假设其它未匹配序列都是最大长度的序列的子序列,故能验证启发函数的正确性
代码
#include <bits/stdc++.h>
#define x first
#define y second
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define PI acos(-1)
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef pair<PII, int> PIII;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
const double eps = 1e-6;
int dx[] = {0, -1, 0, 1, 0}, dy[] = {1, 0, -1, 0, 0};
const int INF = 0x3f3f3f3f, mod = 1e9 + 7;
const int N = 110;
int n;
string s[N];
char op[5] = {"ACGT"};
int pos[N];
int f(){
int ans = 0;
for (int i = 0; i < n; i ++){
int len = s[i].size();
ans = max(ans, len - pos[i]);
}
return ans;
}
bool dfs(int u, int max_depth){
if (f() + u > max_depth) return false;
if (f() == 0) return true;
int t[100];
for (int i = 0; i < 4; i ++){
bool ok = false;
for (int j = 0; j < n; j ++) t[j] = pos[j];//记录现场
for (int j = 0; j < n; j ++){
if(pos[j] == s[j].size()) continue;
if(s[j][pos[j]] == op[i]){
pos[j] ++;
ok = true;
}
}
if (ok){//只有匹配成功一次我们才能继续搜索
if (dfs(u + 1, max_depth)) return true;
for (int j = 0; j < n; j ++) pos[j] = t[j];//恢复现场
}
}
return false;
}
signed main(){
int T;
cin >> T;
while (T--){
memset(pos, 0, sizeof(pos));
cin >> n;
for (int i = 0; i < n; i ++) cin >> s[i];
int depth = 0;
while (!dfs(0, depth)) depth ++;
cout << depth << endl;
}
return 0;
}
标签:HDU,return,int,pos,++,depth,1560,define,IDA 来源: https://blog.csdn.net/m0_52007929/article/details/122285838