题目链接

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

思路:先将字符转换为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. 20 October in ss

    Contest A: sum 快速读. B: 鬼谷子的钱袋(coin) 贪心. 按照类似二进制的方式准备钱袋:1, 2, 4, 8, ... 以此装入的钱袋数目记为 \(N\). 如果最后剩余不足以凑 ...

  2. 信息安全-威胁防御系统-Fortinet:Fortinet

    ylbtech-信息安全-威胁防御系统-Fortinet:Fortinet Fortinet 是多层威胁防御系统的创新者和先锋.该系统能够为业务通信提供最佳安全.优秀性能和低总体占用成本. Forti ...

  3. ! Failed at the chromedriver@2.35.0 install script.

    npm install 过程中报错 解决方法 运行 npm install chromedriver --chromedriver_cdnurl=http://cdn.npm.taobao.org/d ...

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

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

  5. HDU 3466 Proud Merchants(01背包)

    题目链接: 传送门 Proud Merchants Time Limit: 1000MS     Memory Limit: 65536K Description Recently, iSea wen ...

  6. select 和 order by

    select 的优先级要高于order by,相当于是select先创建了一个临时表,再通过临时表去排序.所以,对于一些sum()的汇总,在进行排序,实际是排序的select后的字段,而不是表里的那个 ...

  7. [Git 系列] WIN7下Git的安装

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/monkey7777/article/details/32155833 1.下载git win7版本号 ...

  8. Docker镜像仓库的搭建--> Harbor篇

    简介 Harbor是VMware公司开源的一个企业级Docker Registry项目,项目地址:https://github.com/goharbor/harbor Harbor作为一个企业级私有R ...

  9. arcpy脚本使用多接图表图斑对对应多幅影像进行裁边处理

    插个广告,制作ArcGIS的Tool工具学习下面的教程就对了: 零基础学习Python制作ArcGIS自定义工具观看链接 <零基础学习Python制作ArcGIS自定义工具>课程简介 先将 ...

  10. Android No static field XXX of type I in class Lcom/XXX/R$id错

    问题复现: 问题原因: 出现这样的情况,你先检查你的依赖工程(module)的对应布局layout/xxx.xml是否跟主项目的layout重名,你点开R文件的时候,你会发现你的布局发生了错乱,导致你 ...