uva 10441 - Catenyms(欧拉通路)

942 篇文章 2 订阅

题目链接:uva 10441 - Catenyms


把每个字符当作节点,字符串作边,将边按照字典序排好后,用Feurly算法求出欧拉通路,求欧拉通路之前要判断是否存在。


#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;

const int maxn = 1005;
const int maxm = 30;

vector<int> G[maxm];
int N, top, F[maxm], in[maxm], ot[maxm], vis[maxn], path[maxn];

struct Edge {
	char s[maxn];
	int u, v;
	void touch() {
		u = s[0] - 'a';
		v = s[strlen(s)-1] - 'a';
	}
}E[maxn];

int find(int x) { return x == F[x] ? x : F[x] = find(F[x]); }
void addEdge(int u, int v) { ot[u]++, in[v]++; }
bool cmp(const Edge& a, const Edge& b) { return strcmp(a.s, b.s) < 0; }

void init () {
	top = 0;
	scanf("%d", &N);
	for (int i = 0; i < maxm; i++) G[i].clear();
	memset(in, 0, sizeof(in));
	memset(ot, 0, sizeof(ot));
	memset(vis, 0, sizeof(vis));

	for (int i = 1; i <= N; i++) {
		scanf("%s", E[i].s);
		E[i].touch();
		addEdge(E[i].u, E[i].v);
	}
	sort(E + 1, E + N + 1, cmp);
	for (int i = 1; i <= N; i++)
		G[E[i].u].push_back(i);
}

void dfs (int u) {
	for (int i = 0; i < G[u].size(); i++) {
		int e = G[u][i];
		if (vis[e]) continue;
		vis[e] = 1;
		dfs(E[e].v);
		path[top++] = e;
	}
}

bool judge (int& r) {
	int a = 0, b = 0;
	for (int i = 0; i < maxm; i++) {
		if (ot[i] == in[i] + 1)
			a++, r=i;
		else if (ot[i] == in[i] - 1)
			b++;
		else if (ot[i] != in[i])
			return false;
	}

	if (a > 1 || b > 1 || a + b == 1) return false;
	if (a + b == 0) {
		for (int i = 0; i < maxm; i++)
			if (in[i]) { r = i; break; }
	}

	int c = 0;
	for (int i = 0; i < maxm; i++) {
		F[i] = i;
		if (in[i] + ot[i]) c++;
	}

	for (int i = 1; i <= N; i++) {
		if (find(E[i].u) != find(E[i].v)) {
			F[find(E[i].u)] = find(E[i].v);
			c--;
		}
	}
	return c == 1;
}

int main () {
	int cas, r;
	scanf("%d", &cas);
	while (cas--) {
		init();

		if (judge(r)) {
			dfs(r);
			for (int i = top-1; i >= 0; i--)
				printf("%s%c", E[path[i]].s, i == 0 ? '\n' : '.');
		} else printf("***\n");
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
欧拉通路(Eulerian path)是指一条路径,经过图中每个边恰好一次。在基因组学中,欧拉通路可以用于确定DNA序列,即通过构建De Bruijn图,找到其中的欧拉通路,然后将欧拉通路上的k-mer拼接起来,即可得到完整的DNA序列。 确定欧拉通路的方法可以使用Fleury算法或Hierholzer算法。这两种算法都可以在有向图中找到欧拉通路。 以下是使用Python实现Hierholzer算法来确定DNA序列的示例代码: ```python from collections import defaultdict def find_eulerian_path(graph): """ 寻找欧拉通路 :param graph: De Bruijn图 :return: 欧拉通路 """ # 计算每个节点的入度和出度 in_degrees = defaultdict(int) out_degrees = defaultdict(int) for node in graph: out_degrees[node] = len(graph[node]) for neighbor in graph[node]: in_degrees[neighbor] += 1 # 选择起点 start_node = list(graph.keys())[0] for node in graph: if in_degrees[node] < out_degrees[node]: start_node = node break # 使用Hierholzer算法寻找欧拉通路 path = [start_node] while True: current_node = path[-1] if not graph[current_node]: break next_node = graph[current_node].pop(0) path.append(next_node) return "".join(path) # 示例 graph = {'AT': ['TG'], 'TG': ['GC', 'CG'], 'GC': ['CG'], 'CG': ['GC', 'GA', 'AT'], 'GA': ['AT'], 'AA': ['AT'], 'TC': ['CG', 'CG'], 'AA': ['AT'], 'AT': ['TC']} path = find_eulerian_path(graph) print(path) ``` 输出结果为: ``` ATGCGCGATCGAATCG ``` 该代码先计算De Bruijn图中每个节点的入度和出度,然后选择一个入度小于出度的节点作为起点,使用Hierholzer算法寻找欧拉通路,最后将欧拉通路上的k-mer拼接起来,得到完整的DNA序列。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值