题目链接

题意:每次给出两个字母 和 只有这两个字母的原字符串的子序列,最后让你输出原字符串。

思路:先将字符转换为hash值,然后再转换成图,就是一个拓扑排序了,然后满足不了的情况有两种,一个是构造不了给出的n字符串大小,还有就是字母去重后多了。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
using namespace std;
int n,m;
char s[];
vector<int>G[];
int du[];
string ans="";
int num[];
bool topo()
{
queue<int> q;
for(int i=;i<;i++)
{
//printf("%d ",du[i*10000+1]);
if(num[i]>&&du[i*+]==)
{
q.push(i*+);
}
}
// printf("\n");
while(!q.empty())
{
int u=q.front();
q.pop();
ans+=((u-)/+'a');
for(int i=;i<G[u].size();i++)
{
if(--du[G[u][i]]==)
q.push(G[u][i]);
}
}
// cout<<ans<<endl;
return n==ans.size();
}
int main()
{
scanf("%d%d",&n,&m);
int M;
memset(num,-,sizeof(num));
M=(m-)*m/;
while(M--)
{
char a,b;
char w[];
int len;
scanf("%s",w);
scanf("%d",&len);
if(len==)
continue;
scanf("%s",s);
int suma=;
int sumb=;
int pre=-;
a=w[];
b=w[];
for(int i=;i<len;i++)
{
int id=;
if(s[i]==a)
{
suma++;
id=(s[i]-'a')*+suma;
}
if(s[i]==b)
{
sumb++;
id=(s[i]-'a')*+sumb;
}
if(pre!=-)
{
// printf("id:%d\n",id);
du[id]++;
G[pre].push_back(id);
}
pre=id;
}
if(num[a-'a']==-)
num[a-'a']=suma;
if(num[b-'a']==-)
num[b-'a']=sumb;
}
int sum=;
for(int i=;i<m;i++)
{
sum+=num[i];
}
if(sum!=n){
printf("-1\n");
// printf("000");
}
else{
if(topo())
{
cout<<ans<<endl;
}
else
printf("-1\n");
}
}

subsequence 2的更多相关文章

  1. [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

  2. [LeetCode] Is Subsequence 是子序列

    Given a string s and a string t, check if s is subsequence of t. You may assume that there is only l ...

  3. [LeetCode] Wiggle Subsequence 摆动子序列

    A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...

  4. [LeetCode] Increasing Triplet Subsequence 递增的三元子序列

    Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...

  5. [LeetCode] Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  6. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  7. 【LeetCode】Increasing Triplet Subsequence(334)

    1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists o ...

  8. CF724D. Dense Subsequence[贪心 字典序!]

    D. Dense Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  9. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

  10. [tem]Longest Increasing Subsequence(LIS)

    Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...

随机推荐

  1. Python基础教程(013)--一行代码不要做多件事情

    前言 学习代码编写风格 内容 语法错误报错的信息 SystaxErrot:invalid syntax 语法错误, invalid 无效 每行代码负责完成一个动作 阅读代码,从左至右,从上到下. 学习 ...

  2. CF 1172E Nauuo and ODT ——LCT

    题目:http://codeforces.com/contest/1172/problem/E LCT好题. 考虑对每个颜色求出 “不是该颜色的点组成的连通块的 siz2 之和” .每个颜色用 LCT ...

  3. Key Set

    http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1011&cid=594 Key Set Time Limit: 2000 ...

  4. Http协议面试题(总结)

    Http协议面试题(总结) 一.总结 一句话总结: 主要考常见的状态码,以及https,其它的多抓抓包就熟悉了 1.说一下什么是Http协议? 数据传输的格式规范:对器客户端和 服务器端之间数据传输的 ...

  5. appium常见问题02_android内嵌H5页(webview)如何定位

    现在大多数app都是由原生页面和内嵌H5(即webview)组成,app原生页面直接定位即可,那内嵌H5页面要如何定位呢. 相信大多数人用appium做自动化时都有遇到这个问题,小编总结了下工作中该问 ...

  6. day 90 RBAC

    参考博客 -陈晓梅 http://www.cnblogs.com/c-x-m/p/9025478.html 登录view from django.shortcuts import render,red ...

  7. how to prevent lowmemorykiller from killing processes

    Hi there, I've upgraded a number of test systems to the latest Saucy beta. I've seen quite a few cas ...

  8. HDU 1387 Team Queue( 单向链表 )

    Team Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  9. Java开发中的23种设计模式详解(2)结构型

    我们接着讨论设计模式,上篇文章我讲完了5种创建型模式,这章开始,我将讲下7种结构型模式:适配器模式.装饰模式.代理模式.外观模式.桥接模式.组合模式.享元模式.其中对象的适配器模式是各种模式的起源,我 ...

  10. bootstrap-thymeleaf-分页

    1.HTML代码 <div th:fragment="paginater"> <ul th:id="paginaterUlID" th:if= ...