subsequence 2
题意:每次给出两个字母 和 只有这两个字母的原字符串的子序列,最后让你输出原字符串。
思路:先将字符转换为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的更多相关文章
- [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- [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 ...
- [LeetCode] Wiggle Subsequence 摆动子序列
A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...
- [LeetCode] Increasing Triplet Subsequence 递增的三元子序列
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- 【LeetCode】Increasing Triplet Subsequence(334)
1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists o ...
- CF724D. Dense Subsequence[贪心 字典序!]
D. Dense Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
- [tem]Longest Increasing Subsequence(LIS)
Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...
随机推荐
- codeforces 848A - From Y to Y(构造)
原题链接:http://codeforces.com/problemset/problem/848/A 题意:让我们构造一个字符串.这里有一种操作:取走这个字符串的若干部分,分成两部分,然后将这两部分 ...
- 二、制作BOM表格--物料表格--Bill of Materials
二.制作BOM表格--物料表格--Bill of Materials 公司会根据这个表格进行相关元器件的采购--以及后期的贴片上彩 操作: .dsn--Tools--Bill of Materials ...
- dumpsys, traceView调试命令
1. dumpsys dumpsys cpuinfo: 打印cpu使用情况: dumpsys meminfo: 打印内存使用率情况: dumpsys activity: 打印所有活动的信息: dump ...
- C# 简单软件有效期注册的实现
◆需求:公司一直以来对开发的产品都没有进行使用时间的控制,要么就是将日期限制写死在程序里面,每次都要编译新程序再发给客户,很不方便.于是公司让我写个模块,要求如下:1.无论哪个新开发的程序只要调用这个 ...
- Linux 虚拟机通过NAT模式访问外网
1.配置本地VM8地址 2.配置虚拟机NAT网卡:设置VM8网卡地址和Linux主机相同网段地址,网关随便设置 3.编译网卡文件 vim /etc/sysconfig/network-scri ...
- LINUX为什么要进行内核移植 内核移植的作用
LINUX为什么要进行内核移植 内核移植的作用,不移植能用么? LZ的问题应该是为什么要重新编译内核吧.既然你已经可以跑了,证明你现在用的内核已经移植到你用的硬件上,自然你也不需要做什么移植.通常 ...
- 网络安全随笔 - Linux的netstat查看端口 0.0.0.0与127.0.0.1的区别
linux运维都需要对端口开放查看 netstat 就是对端口信息的查看 # netstat -nltp p 查看端口挂的程序 [root@iz2ze5is23zeo1ipvn65aiz ~]# n ...
- mongodb写入策略(WriteConcern)
写入策略(WriteConcern) mongodb的写入策略有多种方式,写入策略是指当客户端发起写入请求后,数据库什么时候给应答,mongodb有三种处理策略:客户端发出去的时候,服务器收到请求的时 ...
- CET-6 分频周计划生词筛选(Week 1)
Week 1 2016.09.03 p17 bias = prejudice / prejudge p18 diminish p19 distinguish/extinguish + majority ...
- [LeetCode] 196.删除重复的电子邮箱
编写一个 SQL 查询,来删除 Person 表中所有重复的电子邮箱,重复的邮箱里只保留 Id 最小 的那个. +----+------------------+ | Id | Email | +-- ...