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 为底层机制 ⇒ 自动排序(关于键值): ...
随机推荐
- 配置自己的sublime
我配置的sublime的是这样的,就是在input里输入数据,然后在output里可以得到数据,这样比较方便,看到有的大神还配置背景和其他的,有时间搞一下: 首先把编译器g++配置到环境变量,可以从d ...
- 身边的人工智能&人工智能发展史
智能家具 扫地机器人 智能音箱 个人助手 在线翻译 谷歌翻译 微软翻译 YouTube 视频翻译 图像识别 人脸识别 AI+摄像头 下棋高手 Alphago 2017年打败柯洁 成为世界第一 Alph ...
- 实验4&5
[实验任务四]: 在上网时,我们经常会看到以下这种对话框,要用户输入一个验证码. 1.程序设计思想 先利用Math.random()得到一个整数,然后将其类型转换为字符类型,连接起来生成六位验证字符串 ...
- C语言的字符串类型
C语言使用指针来管理字符串(1)C语言中定义字符串的方法:char *p = "linux";此时p就叫字符串,但是实际上p只是一个字符指针(本质上就是一个指针变量,只是p指向了一 ...
- UVA 10534 LCS变种题
求一个序列中 的2*n-1个数字 ,前n+1个数字为严格升序 后n+1个为严格降序,求最长的长度 一开始还没想清楚怎么解,其实就是一个LCS问题,从头到尾以及反序求一下LCS 由于 d[i]为包含了自 ...
- 从认证到调度,K8s 集群上运行的小程序到底经历了什么?
导读:不知道大家有没有意识到一个现实:大部分时候,我们已经不像以前一样,通过命令行,或者可视窗口来使用一个系统了. 前言 现在我们上微博.或者网购,操作的其实不是眼前这台设备,而是一个又一个集群.通常 ...
- 无车承运前世今生,5G货运管家期待您的加入
历时三年的无车承运人试点工作结束,从2020年1月1日起,将执行新的暂行<办法>,在这样一个承前启后的阶段,无车承运人的命运如何?网络货运经营者又是何物? 在新赛道下,将迎来什么样的机遇和 ...
- 吴裕雄--天生自然 PHP开发学习:数组排序
<?php $cars=array("Volvo","BMW","Toyota"); sort($cars); print_r($ca ...
- 计算机网络(3): ICMP报文
- Python笔记_第四篇_高阶编程_实例化方法、静态方法、类方法和属性方法概念的解析。
1.先叙述静态方法: 我们知道Python调用类的方法的时候都要进行一个实例化的处理.在面向对象中,一把存在静态类,静态方法,动态类.动态方法等乱七八糟的这么一些叫法.其实这些东西看起来抽象,但是很好 ...