题目链接

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

思路:先将字符转换为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. Delphi界面篇之ListView控件

    //增加项或列(字段) ListView1.Clear; ListView1.Columns.Clear; ListView1.Columns.Add; ListView1.Columns.Add; ...

  2. Oracle or Question Solve(一)

    Oracle查看版本命令:select * from v$version; ORACLE_BASE和ORACLE_HOME路径查看su - oracleecho $ORACLE_BASEecho $O ...

  3. [NOIP模拟20]题解

    来自达哥的问候…… A.周 究级难题,完全不可做QAQ #include<cstdio> #include<iostream> #include<cstring> ...

  4. 微信网页开发调用微信jssdk接口遇到的坑以及最终解决方法 (持续更新)

    1.微信网页开发调用jssdk时报permission denied 大致是两个原因 (1)首先注册时未将你所调用的接口名字添加至jsApiList (2)第二个就是你的这个公众号没有权限使用这个ap ...

  5. B-Tree, B+Tree, B*树介绍

    [数据结构]B-Tree, B+Tree, B*树介绍 转   [数据结构]B-Tree, B+Tree, B*树介绍 [摘要] 最近在看Mysql的存储引擎中索引的优化,神马是索引,支持啥索引.全是 ...

  6. 热经-北京中地时空数码科技有限公司-研发工程师(WEBGIS方向)

    一面: 登记,填写个人信息 笔试 选择题: HTML,CSS,JS 的选择题,都是基础题.其中有一道问哪个不是 document 的属性或方法,我在 bgColor 和 focus() 上面纠结了一下 ...

  7. 113、TensorFlow变量集合

    #一个tensorflow程序断开的部分可能要创建变量 # 如果有一种方法来访问所有的变量是非常有用的 #因为这个原因TensorFlow提供了集合,是一些张量的集合 #或者是其他的对象,就像tf.V ...

  8. Maximum of lines in a DataBand

    Hello! I have a problem.I have a DataBand, but I need it to grow only up to 14 lines. If it is beyon ...

  9. 斯坦福【概率与统计】课程笔记(四):EDA | 茎叶图

    茎叶图的只做方法如下: 将每个数字分成茎和叶 对所有茎排序,并纵向从小到大放置好 对相同茎下的叶归到一起并排序,垂直于茎的排列方向放置好 举个例子:我们有一份奥斯卡影后的年龄集合: 34 34 27 ...

  10. linux下用户切换

    Linux学习使用ubuntu17,ubuntu安装的时候没有超级用户root的密码. 设置系统root用户的密码,Ubuntu刚安装后,因为root没有默认密码,需要手动设定.以安装ubuntu时输 ...