Codeforces Round #547 (Div. 3)D. Colored Boots
D. Colored Boots
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Input
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.
Output
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.
题意:两个字符串,两两字符匹配条件是在不同字符串中且相等,'?'可以与任何字符匹配,求最大匹配并输出两个匹配字符位置
思路:贪心,用vector存出现字符的位置,首先是普通字符(都不含'?')能匹配就匹配,之后是第一个字符串中'?'与剩下第二个字符串中还剩下的遍历到就匹配,因为?字符和任何都行,之后第二个字符串中'?'与剩下第一个字符串中还剩下的遍历到就匹配
// luogu-judger-enable-o2
#include<bits/stdc++.h>
#include<unordered_set>
#define rg register ll
#define inf 2147483647
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define ll long long
#define maxn 200005
const double eps = 1e-8;
using namespace std;
inline ll read()
{
char ch = getchar(); ll s = 0, w = 1;
while (ch < 48 || ch>57) { if (ch == '-')w = -1; ch = getchar(); }
while (ch >= 48 && ch <= 57) { s = (s << 1) + (s << 3) + (ch ^ 48); ch = getchar(); }
return s * w;
}
inline void write(ll x)
{
if (x < 0)putchar('-'), x = -x;
if (x > 9)write(x / 10);
putchar(x % 10 + 48);
}
vector<ll>a[30],b[30];
vector<pair<ll,ll>>p;
int main()
{
ll n=read();
for(rg i=1;i<=n;i++)
{
char s;
cin>>s;
//cout<<s<<endl;
if(s=='?')a[26].push_back(i);
else a[s-'a'].push_back(i);
}
//getchar();
for(rg i=1;i<=n;i++)
{
char s;
cin>>s;
//cout<<s<<endl;
if(s=='?')b[26].push_back(i);
else b[s-'a'].push_back(i);
}
ll ans=0;
for(rg i=0;i<26;i++)
{
//cout<<a[i].size()<<" "<<b[i].size()<<endl;
if(a[i].size()&&b[i].size())
{
ll k=min(a[i].size(),b[i].size());
ans+=k;
for(rg j=0;j<k;j++)
{
p.push_back(make_pair(a[i][j],b[i][j]));
}
a[i].erase(a[i].begin(),a[i].begin()+k);
b[i].erase(b[i].begin(),b[i].begin()+k);
}
}
//cout<<ans<<"a"<<endl;
//cout<<1<<endl;
for(rg i=0;i<26;i++)
{
if(b[i].size())
{
ll k=min(a[26].size(),b[i].size());
ans+=k;
for(rg j=0;j<k;j++)
{
p.push_back(make_pair(a[26][j],b[i][j]));
}
a[26].erase(a[26].begin(),a[26].begin()+k);
b[i].erase(b[i].begin(),b[i].begin()+k);
}
}
// cout<<1<<endl;
for(rg i=0;i<26;i++)
{
if(a[i].size())
{
ll k=min(b[26].size(),a[i].size());
ans+=k;
for(rg j=0;j<k;j++)
{
p.push_back(make_pair(a[i][j],b[26][j]));
}
a[i].erase(a[i].begin(),a[i].begin()+k);
b[26].erase(b[26].begin(),b[26].begin()+k);
}
}
if(a[26].size()&&b[26].size())
{
ll k=min(b[26].size(),a[26].size());
ans+=k;
for(rg j=0;j<k;j++)
{
p.push_back(make_pair(a[26][j],b[26][j]));
}
a[26].erase(a[26].begin(),a[26].begin()+k);
b[26].erase(b[26].begin(),b[26].begin()+k);
}
cout<<ans<<endl;
for(auto it:p)
{
cout<<it.first<<" "<<it.second<<endl;
}
return 0;
}
- linux学习第四十八篇:php-fpm的pool,php-fpm慢执行日志,定义open_basedir,php-fpm进程管理
- linux学习第五十一篇:NFS介绍,NFS服务端安装配置,NFS配置选项
- linux学习第五十二篇: exportfs命令,NFS客户端问题,FTP介绍,使用vsftpd搭建ftp服务
- linux学习第五十四篇:Tomcat介绍,安装jdk,安装Tomcat
- linux学习第五十九篇:LVS DR模式搭建,keepalived lvs
- linux学习第五十四篇:配置Tomcat监听80端口,配置Tomcat的虚拟主机,Tomcat日志
- linux学习第五十六篇:集群介绍,keepalived介绍,用keepalived配置高可用集群
- linux学习第五十八篇: 负载均衡集群介绍,LVS介绍,LVS的调度算法,LVS NAT模式搭建
- Python中eval带来的潜在风险,你知道吗?
- React Native自定义导航条
- android混淆那些坑
- 微信小程序开发入门篇
- Support Annotation Library使用详解
- React Native之Navigator
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 04 CentOS6.5系统语言切换为中文
- 【SpringBoot DB 系列】Redis 高级特性之 Bitmap 使用姿势及应用场景介绍
- 踩坑:一次年轻代GC长暂停问题的解决与思考
- 监听MySQL的binlog日志工具分析:Canal
- 小解c# foreach原理
- 3分钟短文:任命管理员,给Laravel普通用户提权
- this到底是什么?
- ES5面向对象基础
- 面试官问我啥是OAuth 2.0,两个案例讲懂他~
- 年轻代频繁ParNew GC,导致http服务rt飙高
- JWT 使用 nimbus-jose-jwt 进行解码
- linux centos7 编译安装python3 --shell脚本
- Qt音视频开发36-USB摄像头解码qcamera方案
- JS中属性描述符
- CSS3实现彩色炫酷文字