• Codeforces Round #288 (Div. 2) D. Tanya and Password 欧拉通路


    题目链接:

    题目

    D. Tanya and Password
    time limit per test 2 seconds
    memory limit per test 256 megabytes
    inputstandard input
    outputstandard output

    问题描述

    While dad was at work, a little girl Tanya decided to play with dad's password to his secret database. Dad's password is a string consisting of n + 2 characters. She has written all the possible n three-letter continuous substrings of the password on pieces of paper, one for each piece of paper, and threw the password out. Each three-letter substring was written the number of times it occurred in the password. Thus, Tanya ended up with n pieces of paper.

    Then Tanya realized that dad will be upset to learn about her game and decided to restore the password or at least any string corresponding to the final set of three-letter strings. You have to help her in this difficult task. We know that dad's password consisted of lowercase and uppercase letters of the Latin alphabet and digits. Uppercase and lowercase letters of the Latin alphabet are considered distinct.

    输入

    The first line contains integer n (1 ≤ n ≤ 2·105), the number of three-letter substrings Tanya got.

    Next n lines contain three letters each, forming the substring of dad's password. Each character in the input is a lowercase or uppercase Latin letter or a digit.

    输出

    If Tanya made a mistake somewhere during the game and the strings that correspond to the given set of substrings don't exist, print "NO".

    If it is possible to restore the string that corresponds to given set of substrings, print "YES", and then print any suitable password option.

    样例

    input
    5
    aca
    aba
    aba
    cab
    bac

    output
    YES
    abacaba

    题意

    给你n个串每个串三个字母,问你存不存在一个长度为n+2的串,使得上面输入的串刚好是这个串的所有的长度为第三的子串。

    题解

    对于输入xyz,xy建一个节点,yz建一个节点,然后xy连一条边到yz,跑欧拉通路。

    代码

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<algorithm>
    #include<vector>
    #include<string>
    using namespace std;
    
    const int maxn = 10101;
    int n;
    
    vector<int> G[maxn];
    int in[maxn], out[maxn];
    int mp[256];
    string ans;
    int cnt[maxn];
    
    char rmp[111];
    void init() {
    	for (int i = 'a'; i <= 'z'; i++) {
    		mp[i] = i - 'a'; rmp[i - 'a'] = i;
    	}
    	for (int i = 'A'; i <= 'Z'; i++) {
    		mp[i] = i - 'A' + 26; rmp[i - 'A' + 26] = i;
    	}
    	for (int i = '0'; i <= '9'; i++) {
    		mp[i] = i - '0' + 52; rmp[i - '0' + 52] = i;
    	}
    	memset(in, 0, sizeof(in));
    	memset(out, 0, sizeof(out));
    	memset(cnt, 0, sizeof(cnt));
    }
    //有向图欧拉通路
    void dfs(int u) {
    	while (cnt[u] < G[u].size()) dfs(G[u][cnt[u]++]);
    	ans += rmp[u % 62];
    }
    
    int main() {
    	init();
    	scanf("%d", &n);
    	char str[22];
    	for (int i = 0; i < n; i++) {
    		scanf("%s", str);
    		int u = mp[str[0]] * 62 + mp[str[1]];
    		int v = mp[str[1]] * 62 + mp[str[2]];
    		G[u].push_back(v);
    		in[v]++, out[u]++;
    	}
    	int st=-1,ed=-1,tmp=0;
    	int su = 1,cnt=0;
    	for (int i = 0; i < maxn; i++) {
    		if (abs(in[i] - out[i]) >= 2) { su = 0; break; }
    		if (in[i] - out[i] == 1) {
    			if (ed != -1) { su = 0; break; }
    			ed = i;
    		}
    		else if (in[i] - out[i] == -1) {
    			if (st != -1) { su = 0; break; }
    			st = i;
    		}
    		if (in[i] != out[i]) cnt++;
    		if (in[i] == out[i] && in[i]>0) tmp = i;
    	}
    	if (cnt == 0) { st = tmp; }
    	if (su) {
    		dfs(st);
    		ans.push_back(rmp[st/62]);
    		if (ans.length() == n + 2) {
    			reverse(ans.begin(), ans.end());
    			cout << "YES" << endl;
    			cout << ans << endl;
    		}
    		else {
    			cout << "NO" << endl;
    		}
    	}
    	else cout << "NO" << endl;
    	return 0;
    }
  • 相关阅读:
    C# 实现将 PDF 转文本的功能
    推荐大家使用的CSS书写规范、顺序
    Android 应用开发者必看的 9 个 Tips
    网站统计中的数据收集原理及实现
    JSON劫持漏洞攻防原理及演练
    巧妙使用 CSS3 的褪色和动画效果制作消息提醒框
    20条常见的编码陷阱
    11 个最佳 jQuery 滚动条插件
    JavaScript客户端MVC 框架综述
    20个初学者实用的CSS技巧
  • 原文地址:https://www.cnblogs.com/fenice/p/5631598.html
Copyright © 2020-2023  润新知