D. Colored Boots(STL)
There are nn left boots and nn right boots. Each boot has a color which is denoted as a lowercase Latin letter or a question mark ('?'). Thus, you are given two strings ll and rr, both of length nn. The character lili stands for the color of the ii-th left boot and the character riri stands for the color of the ii-th right boot.
A lowercase Latin letter denotes a specific color, but the question mark ('?') denotes an indefinite color. Two specific colors are compatible if they are exactly the same. An indefinite color is compatible with any (specific or indefinite) color.
For example, the following pairs of colors are compatible: ('f', 'f'), ('?', 'z'), ('a', '?') and ('?', '?'). The following pairs of colors are notcompatible: ('f', 'g') and ('a', 'z').
Compute the maximum number of pairs of boots such that there is one left and one right boot in a pair and their colors are compatible.
Print the maximum number of such pairs and the pairs themselves. A boot can be part of at most one pair.
The first line contains nn (1≤n≤1500001≤n≤150000), denoting the number of boots for each leg (i.e. the number of left boots and the number of right boots).
The second line contains the string ll of length nn. It contains only lowercase Latin letters or question marks. The ii-th character stands for the color of the ii-th left boot.
The third line contains the string rr of length nn. It contains only lowercase Latin letters or question marks. The ii-th character stands for the color of the ii-th right boot.
Print kk — the maximum number of compatible left-right pairs of boots, i.e. pairs consisting of one left and one right boot which have compatible colors.
The following kk lines should contain pairs aj,bjaj,bj (1≤aj,bj≤n1≤aj,bj≤n). The jj-th of these lines should contain the index ajaj of the left boot in the jj-th pair and index bjbj of the right boot in the jj-th pair. All the numbers ajaj should be distinct (unique), all the numbers bjbj should be distinct (unique).
If there are many optimal answers, print any of them.
10
codeforces
dodivthree
5
7 8
4 9
2 2
9 10
3 1
7
abaca?b
zabbbcc
5
6 5
2 3
4 6
7 4
1 2
9
bambarbia
hellocode
0
10
code??????
??????test
10
6 2
1 6
7 3
3 5
4 8
9 7
5 1
2 4
10 9
8 10
题解:用vector记录a数组中每个字符和其出现的位置,a数组中问号的位置。定义查找函数:bool Find(int s, int x)//在a数组里找与b数组中下标为x对应的字符s。
遍历b数组,ans的pair vector记录答案,在Find函数中记录~~看代码实现:
感受:一开始不太熟悉vector,做完这道题感觉自己学到了很多东西,还有能力有了很大的提升呢~~
#include <iostream>
#include<vector>
using namespace std;
int n;
char a[];
char b[];
vector<int> v[], sp;//v[i]记录字符ASCII码值为i的位置,sp记录问号的位置
vector<pair<int, int> > ans;//ans记录答案,pair分别对应a,b所对应的位置
bool Find(int s, int x)//在a数组里找与b数组中下标为x对应的字符s
{
if (v[s].empty())//如果a数组里面没有对应的字符s了(ASCII码转换),就是没找到
return ;
ans.emplace_back(v[s].back(), x);//记录字符在第一个串和第二个串的位置
v[s].pop_back();//将记录的位置去掉
return ;
}
int main()
{
cin >> n;
for (int i = ; i <= n; i++)//字符串为了方便用下标来输入
{
cin >> a[i];
v[a[i]].push_back(i);//将每一个a[i]字符对应的位置放入v的vector
}
for (int i = ; i <= n; i++)
cin >> b[i];
for (int i = ; i <= n; i++)//进行遍历
{
if (b[i] == '?')//记录问号的位置
sp.push_back(i);
else if (!Find(b[i], i))//如果不是问号,并且a中没有对应的字符(不包括问号)
Find('?', i);//就将1个问号与之匹配
}
for (vector<int>::iterator it = sp.begin(); it != sp.end(); it++)//遍历问号的位置
{
for (int j = ; j < ; j++)//找到一个字符(任意)与问号对应
{
if (Find(j, *it))
break;
}
}
cout << ans.size() << endl;//总个数
for(vector<pair<int,int> >::iterator it=ans.begin();it!=ans.end();it++)//遍历
cout << (*it).first << " " <<(*it).second << endl;
return ;
}
D. Colored Boots(STL)的更多相关文章
- C++标准模板库(STL)和容器
		1.什么是标准模板库(STL)? (1)C++标准模板库与C++标准库的关系 C++标准模板库其实属于C++标准库的一部分,C++标准模板库主要是定义了标准模板的定义与声明,而这些模板主要都是 类模板 ... 
- 【常用技巧】标准模板库(STL)
		[常用技巧]标准模板库(STL) 在前几个章节中我们已经使用了诸如队列.堆.堆栈.vector 等标准模板库中的模板,切身感受到了它给我们带来的极大便利.在本节中,我们还要介绍两种标准模板——stri ... 
- C++ 标准模板库(STL)——容器(Containers)的用法及理解
		C++ 标准模板库(STL)中定义了通用的模板类和函数,这些模板类和函数可以实现多种流行和常用的算法和数据结构,如向量(vector).队列(queue).栈(stack).set.map等.这次主要 ... 
- Three.js 3D打印数据模型文件(.STL)载入中
		3DPrint是现在和未来10年度科技产品的主流之中.广泛的. 对于电子商务类3D打印网站.一个主要功能就是商品3D呈现的方式,那是,3D数据可视化技术. HTML5(WebGL)它可以用于构建3D查 ... 
- C++标准模板库(STL)之Vector
		在C中,有很多东西需要自己实现.C++提供了标准模板库(Standard Template Libray,STL),其中封装了很多容器,不需要费力去实现它们的细节而直接调用函数来实现功能. 具体容器链 ... 
- C++ 标准库和标准模板库(STL)
		转自原文http://blog.csdn.net/sxhelijian/article/details/7552499 一.C++标准库 C++标准库的内容分为10类,分别是(建议在阅读中,将你已经用 ... 
- 离散化——化不可能为可能(STL)
		所谓离散,就是化连续为不连续,使得我们某种枚举的方法得以实现. 当然,离散还能够帮助我们将某些数据范围很大达到2^16,但是这些数据并不多(例如才1000+),我们可以把数据进行离散,保持他们之间的相 ... 
- C++ 标准模板库介绍(STL)
		1. STL 基本介绍 C++ STL(标准模板库)是惠普实验室开发的一系列软件的统称,是一套功能强大的 C++ 模板类.STL的目的是为了标准化组件,这样就不用重新开发,让后来者可以使用现成的组件, ... 
- 标准模板库(STL) map —— 初始化问题
		map 容器没有:.reverse成员: map 是关联式容器,会根据元素的键值自动排序: map 容器不是连续的线性空间: 标准 STL 使用 RB-tree 为底层机制 ⇒ 自动排序(关于键值): ... 
随机推荐
- Neo4j--常用的查询语句
			参考 https://www.w3cschool.cn/neo4j 准备工作 插入一堆朝代节点 插入我大明皇帝节点 创建大明皇帝统治大明王朝的关系 看一下结果 WHERE WHERE 语法 WHERE ... 
- JVM探秘:jstat查看JVM统计信息
			本系列笔记主要基于<深入理解Java虚拟机:JVM高级特性与最佳实践 第2版>,是这本书的读书笔记. jstat命令用来查看JVM统计信息,可以查看类加载信息.垃圾收集的信息.JIT编译信 ... 
- winform显示、隐藏任务栏及开始菜单
			private const int SW_HIDE = 0; //隐藏 private const int SW_RESTORE = 9;//显示 /// <summary> /// 获取 ... 
- UVALive 4670 AC自动机
			第二道AC自动机的题目了,之前参考的是网上一个博客算法,不怎么好,难写而且占空间 后来参照大白书做的这题,代码简洁多了 #include <iostream> #include <c ... 
- 更新anaconda包
			升级安装python环境后, 把老的包重新安装回去. ls -l /opt/anaconda3/lib/python3.7/site-packages/ | grep "\-info&quo ... 
- selenium爬取优酷页面并下载图片
			from selenium import webdriver import requests driver = webdriver.Chrome() #打开优酷 driver.get("ht ... 
- Property or method "cancleInput" is not defined on the instance but referenced during render.
			因为我的点击事件,是动态添加上去的 报错如标题 [Vue warn]: Property or method "cancleInput" is not defined on th ... 
- Maven--配置 Maven 从 Nexus 下载构件
			在 POM 中配置: <project> ... <repositories> <repository> <id>nexus</id> &l ... 
- 使用java(jdbc)向mysql中添加数据时出现“unknown column……”错误
			错误情况如题,出现这个错误的原因是这样的: 在数据库中,插入一个字符串数据的时候是需要用单引号引起来的. 而下面的代码,注意看: sta.executeUpdate("INSERT INTO ... 
- Tomcat server.xml常用配置  含有外带文件及默认host
			<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE server-xml [<!ENTITY ... 
