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&& ...
随机推荐
- LOJ 2302 「NOI2017」整数——压位线段树
题目:https://loj.ac/problem/2302 压30位,a最多落在两个位置上,拆成两次操作. 该位置加了 a 之后,如果要进位或者借位,查询一下连续一段 0 / 1 ,修改掉,再在含有 ...
- CTF | bugku | 字符?正则?
做题链接 一个详细讲正则的网址1 一个详细讲正则的网址2 代码如下 <?php highlight_file('2.php'); $key='KEY{********************** ...
- Cygwin访问windows磁盘目录
http://blog.csdn.net/duguduchong/article/details/7680650 Cygwin访问windows磁盘目录 标签: windows磁盘user平台c 2 ...
- fiddler 暂停抓包
或者点击左下角 capturing
- 如何使用Charles对手机进行抓包
步骤:(如下为ios步骤,安卓类似) 1)Mac安装Charles,保证手机与电脑在同一wifi(若没有WiFi时,Mac.MacBook,etc 自带热点功能) 2)在手机当前连接的wifi设置里配 ...
- __str__和__repr__的区别
有时候我们想让屏幕打印的结果不是对象的内存地址,而是它的值或者其他可以自定义的东西,以便更直观地显示对象内容,可以通过在该对象的类中创建或修改__str__()或__repr__()方法来实现(显示对 ...
- ubantu下关于linux命令合集
ubantu下linux的命令与操作 1.熟悉linux目录是学习linux非常必要的第一步 linux目录结构: linux目录: /:根目录,一般根目录下只存放目录,在Linux下有且只有一个根目 ...
- 二、python基础之列表、元组
一.列表 列表的概念: 列表由一系列按特定顺序排列的元素组成.你可以创建包含字母表中所有字母.数字0-9或所有家庭成员姓名的列表:也可以将任何东西加入列表中,其中的元素之间没有任何关系.鉴于列表通常包 ...
- 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 ...
- java虚拟机规范(se8)——class文件格式(一)
第四章 class文件格式 本章介绍了java虚拟机的class文件格式.每一个class文件包含一个单独的类或者接口的定义.虽然类和接口不一定都定义在文件中(比如类和接口亦可以通过类加载器直接生成) ...