题目链接

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

思路:先将字符转换为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. datatbales 使用笔记

    实例: var datatable_obj = null; $(document).ready(function(){ datatable_obj = $('#merchant-list').Data ...

  2. DB:目录

    ylbtech-DB:目录 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   作者:ylbtech出处:http://ylbtech.cn ...

  3. SharePoint 2013中PerformancePoint仪表板设计器连接Analysis Services 2012的问题

    在SharePoint 2013的PerformancePoint仪表板设计器在创建链接到AnalysisServices 2012的数据链接的时候,数据库列表无法获取服务器上的数据库.这个问题挺让人 ...

  4. linux远程管理器 - xshell和xftp使用教程(zhuan)

    准备好连接linux服务器的工具,推荐用xshell和xftp. xshell 是一个强大的安全终端模拟软件,它支持SSH1, SSH2, 以及Microsoft Windows 平台的TELNET ...

  5. Unity获取Android和iOS手机系统电量及网络状况

    最开始考虑使用中间静态链接库来调用手机系统自带的API,但是在研究的过程中发现Android系统将电量等信息记录在了固定的文件中,所以只需要在C#中直接读取就可以而不需要中间库. a.Android版 ...

  6. HDU 5183 Negative and Positive (NP) (手写哈希)

    题目链接:HDU 5183 Problem Description When given an array \((a_0,a_1,a_2,⋯a_{n−1})\) and an integer \(K\ ...

  7. 斯坦福【概率与统计】课程笔记(五):EDA | 箱线图

    介绍箱线图之前,需要先介绍若干个其需要的术语 min:整个样本的最小值 max:整个样本的最大值 Range:即整个样本的取值范围,Range = max - min Inter-Quartile R ...

  8. Cocos2d Box2D之静态刚体

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. b2_staticBody 在模拟环境下静态物体是不会移动的,就好像有无限大的质量.在Box2D的内部会将质量至反,存储为零.静态物体也可 ...

  9. Can't connect to local MySQL server through socket '/opt/lampp/var/mysql/mysql.sock' (2)

    ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/opt/lampp/var/mysql/mysql.s ...

  10. viewport的深入调研

    1.viewport概念:viewport就是设备的屏幕上能用来显示我们的网页的那一块区域. viewport的默认值980px或1024px等,以下是浏览器的默认viewport宽度 2.css中的 ...