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&& ...
随机推荐
- AT2705 Yes or No(组合数学)
传送门 解题思路 首先将这个模型放到坐标轴上,\(x\)轴表示\(1\),\(y\)轴表示\(0\).问题就转化成了从\((0,0)\)走到\((n,m)\),每次可以猜测向\(x\)轴或向\(y\) ...
- RabbitMQ的简单应用【转】
虽然后台使用了读写分离技术,能够在一定程度上抗击高并发,但是如果并发量特别巨大时,主数据库不能同时处理高并发的请求,这时数据库容易宕机. 问题: 现在的问题是如何既能保证数据库正常运行,又能实现用户数 ...
- [杂题]:B/b(二分答案)
题目传送门(内部题53) 输入格式 第二行$2$个整数表示$n,m$.接下来$m$行每行两个整数,描述一个点对$(x_i,y_i)$. 输出格式 一个整数,表示最短距离. 样例 样例输入: 6 21 ...
- Transform.Find()
代码演示: using System.Collections;using System.Collections.Generic;using UnityEngine; public class Tran ...
- django中常见命令总结
一,创建一个django项目: django-admin.py startproject mysite二,在mysite目录下创建blog应用: python manage.py startapp b ...
- mysql的windows客户端链接远程全套案例
我是linux 的服务器,navicat12的客户端, 开始链接的时候需要开服务器上得对外爆漏端口 3306,方法: 添加指定需要开放的端口: firewall-cmd --add-port=/tcp ...
- Windows下使用Composer安装yii2
Composer简介 Composer 是PHP中用来管理依赖(dependency)关系的工具.你可以在自己的项目中声明所依赖的外部工具库(libraries),Composer会帮你安装这些依赖的 ...
- 转 lsof命令详解
lsof命令详解 lsof (list open files)是一个列出当前系统打开文件的工具.在linux系统环境下,任何事物都可以以文件形式存在,通过文件不仅可以访问常规的数据,还可以访问网络 ...
- tp5 之 "No input file specified
tp5 之 "No input file specified" 问题 通过"域名/模块/控制器/方法"这样的方式访问的时候,浏览器输出如下: 直接通过" ...
- 如何通过HTTP API 调取tushare的股票数据
长久以来,Tushare一直以固定的Python SDK方式为大家提供数据服务. 虽然在基于Python的数据分析和Python的量化策略开发很方便,但习惯用其他语言的同学们表示了“抗议”,于是在Tu ...