HUST 1605 Gene recombination

简单广搜。4进制对应的10进制数来表示这些状态,总共只有(4^12)种状态。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;

const int maxn = 15;
map<int, int>m;
struct P
{
    int state;
    int tot;
};
queue<P>Q;
char s1[maxn], s2[maxn];
int tmp1[maxn], r1, tmp2[maxn], r2;
int A, B;
int n;
int b[maxn];

int f(char sign)
{
    if (sign == ‘A‘) return 0;
    if (sign == ‘T‘) return 1;
    if (sign == ‘G‘) return 2;
    if (sign == ‘C‘) return 3;
}

void init()
{
    while (!Q.empty()) Q.pop();
    m.clear(); A = B = 0;
    for (int i = 0; s1[i]; i++) A = A + f(s1[i])*b[i];
    for (int i = 0; s2[i]; i++) B = B + f(s2[i])*b[i];
}

void BFS()
{
    P now; now.state = A; now.tot = 0; m[A] = 1; Q.push(now);
    while (!Q.empty())
    {
        P head = Q.front(); Q.pop();
        if (head.state == B)
        {
            printf("%d\n", head.tot);
            break;
        }

        memset(tmp1, 0, sizeof tmp1);
        memset(tmp2, 0, sizeof tmp2);

        r2 = r1 = 0; int w = head.state;
        while (w) tmp2[r2++] = tmp1[r1++] = w % 4, w = w / 4;

        swap(tmp1[0], tmp1[1]);
        int new_state = 0;
        for (int i = 0; i < n; i++) new_state = new_state + tmp1[i] * b[i];

        if (m[new_state] == 0)
        {
            m[new_state] = 1;
            P d; d.state = new_state; d.tot = head.tot + 1;
            Q.push(d);
        }

        new_state = 0;
        tmp2[n] = tmp2[0];
        for (int i = 1; i <= n; i++) new_state = new_state + tmp2[i] * b[i - 1];

        if (m[new_state] == 0)
        {
            m[new_state] = 1;
            P d; d.state = new_state; d.tot = head.tot + 1;
            Q.push(d);
        }
    }
}

int main()
{
    b[0] = 1; for (int i = 1; i <= 11; i++) b[i] = 4 * b[i - 1];

    while (~scanf("%d", &n))
    {
        scanf("%s%s", s1, s2);
        init();
        BFS();
    }
    return 0;
}
时间: 2025-01-13 11:42:37

HUST 1605 Gene recombination的相关文章

hust 1605 - Gene recombination(bfs+字典树)

1605 - Gene recombination Time Limit: 2s Memory Limit: 64MB Submissions: 264 Solved: 46 DESCRIPTION As a gene engineer of a gene engineering project, Enigma encountered a puzzle about gene recombination. It is well known that a gene can be considered

HUST 1588 辗转数对

1588 - 辗转数对 时间限制:1秒 内存限制:128兆 155 次提交 27 次通过 题目描述 假设当前有一个数对(a, b),我们可以通过一步将这个数对变为一个新数对(a + b, b)或者是(a, a + b).初始的数对为(1, 1),你的任务是找到一个数字k,即通过最少的步数使得这个数对中至少一个数字等于n. 输入 输入包括多组数据,每组数据包括一行,每行有一个整数n. 输出 每组数据输出一行,每行一个整数n. 样例输入 5 3 样例输出 3 2 提示 第一个样例的方法是 (1,1)

HUST 1698 - 电影院 组合数学 + 分类思想

http://acm.hust.edu.cn/problem/show/1698 题目就是要把一个数n分成4段,其中中间两段一定要是奇数. 问有多少种情况. 分类, 奇数 + 奇数 + 奇数 + 奇数 奇数 + 奇数 + 奇数 + 偶数 偶数 + 奇数 + 奇数 + 奇数 偶数 + 奇数 + 奇数 + 偶数 然后奇数表达成 2 * a - 1这个样子,就能列出方程. 然后就是类似于解a1 + a2 + a3 + a4 = x的问题了. #include <cstdio> #include &l

POJ 1080 Human Gene Functions(LCS)

Description It is well known that a human gene can be considered as a sequence, consisting of four nucleotides, which are simply denoted by four letters, A, C, G, and T. Biologists have been interested in identifying human genes and determining their

HUST 1017 - Exact cover (Dancing Links 模板题)

1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 5584 次提交 2975 次通过 题目描述 There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is a selection of rows such that every column has a 1 in exactly one of the selected rows. Try to find o

勇士出征[HUST 1439]

勇士出征[HUST 1439] 时间1000ms,内存64MB 第十届"北大青鸟"杯浙江师范大学程序设计竞赛 这道题跟UVA-12100是一样的题目.我这里用的是STL的双端队列deque容器配合优先队列priority_queue,写起来会比较轻松:依次将输入压入队列,然后不断扫描队列,符合最大优先级的(优先队列的顶部元素)将其送出,而不再压入队尾.直到找到符合自己的标记的为止. 当然这道题也有用数组使用滚雪球的方式实现的,也就是开一个大的数组,每次将元素后挪时,直接将其放在数组末尾

UVALive 2659+HUST 1017+ZOJ 3209 (DLX

UVALive 2659 题目:16*16的数独.试了一发大白模板. /* * @author: Cwind */ //#pragma comment(linker, "/STACK:102400000,102400000") #include <iostream> #include <map> #include <algorithm> #include <cstdio> #include <cstring> #include

HUST 1341 A - A Simple Task(哈理工 亚洲区选拔赛练习赛)

A - A Simple Task Time Limit:1000MS    Memory Limit:131072KB    64bit IO Format:%lld & %llu SubmitStatusPracticeHUST 1341 Description As is known to all, lots of birds are living in HUST. A bird has s units of food on the first day, and eats k units

hust 1589 找出子串

题目描述 给定一个字符串s ,求出一个子串t,满足如下性质:1.       t是s的一个前缀.2.       t是s的一个后缀.3.       t出现在s的中间(并非前缀和后缀).例如:字符串s为fixprefixsuffix,t可以是fix.字符串s为aaa,t可以是aa.输入 输入包括多组数据,每组数据为一行,每行有一个字符串s,其长度不超过10^6(一百万). 输出 每组数据输出一行,每行为一个字符串t,若不存在字符串t,则输出"Just a legend"(不包括引号).