Colored Boots题解
题目来自Codeforce 1141Dhttp://codeforces.com/problemset/problem/1141/D
因为是全英文题面,就先简单的阐述一下题面。
首先输入一个数n,然后输入两个长度为n的字符串,我们需要做的就是对两个字符串的每个字符从1到n编号,然后对两个字符串的所有字符进行一对一的匹配,如果两个字符串中有字符相同,则匹配成功,输出两个字符的对应编号,其中?可以和任意字符匹配,问最多能匹配多少个字符,输出最优解。
解题思路
这个题如果直接遍历不用说,肯定超时,但是像我这种菜鸡其他的也不会,所以我也是遍历,但是可以说一种优化过的遍历。
首先我用的是结构体来存这两个字符串,这样的话可以保证字符在排序之后也可以和它的编号对应,然后就是对这两个字符串进行从大到小的排序,这样就把?全部放到了最后,只要对前面的字母先对应匹配,再来处理?,就会省不少时间,并且因为前面的字母都是已经排过序的,匹配起来也非常好处理,多余的操作很少,速度自然也就快了。其中我还用了两个bool数组分别对应两个字符串的所有字符,一旦某一个字符被匹配过之后,就利用bool数组对其进行标记,减少后面的处理步骤。cun数组就是用来存放对应的编号的。
完整代码
#include<iostream>
#include<algorithm>
using namespace std;
struct stu
{
int num;
char str;
};
stu a[],b[];
int cun[][];
bool cmp(stu n,stu m)
{
return n.str>m.str;
}
bool str1[],str2[];
int main()
{
int i,j,k=;
int n,sum=;
cin>>n;
for(i=;i<=n;i++)
{
cin>>a[i].str;
a[i].num=i;
}
for(i=;i<=n;i++)
{
cin>>b[i].str;
b[i].num=i;
}
sort(a+,a+n+,cmp);
sort(b+,b+n+,cmp);
i=;
j=;
while()
{
if(a[i].str=='?'||b[j].str=='?'||i>n||j>n)
break;
if(a[i].str==b[j].str)
{
sum++;
cun[sum][]=a[i].num;
cun[sum][]=b[j].num;
str1[a[i].num]=true;
str2[b[j].num]=true;
i++;
j++;
}
else if(a[i].str>b[j].str)
{
while(i<=n)
{
i++;
if(a[i].str<=b[j].str)
break;
}
}
else if(a[i].str<b[j].str)
{
while(j<=n)
{
j++;
if(a[i].str>=b[j].str)
break;
}
}
}
//cout<<i<<" "<<j<<endl;
while(i<=n&&k<=n)
{
if(str2[b[k].num])
{
k++;
continue;
}
if(a[i].str=='?'&&!str1[a[i].num])
{
sum++;
cun[sum][]=a[i].num;
cun[sum][]=b[k].num;
str2[b[k].num]=true;
str1[a[i].num]=true;
k++;
}
i++;
//cout<<i<<" "<<k<<endl;
}
k=; while(j<=n&&k<=n)
{
if(str1[a[k].num])
{
k++;
continue;
}
if(b[j].str=='?'&&!str2[b[j].num])
{
sum++;
cun[sum][]=a[k].num;
cun[sum][]=b[j].num;
str1[a[k].num]=true;
str2[b[j].num]=true;
k++;
}
j++;
//cout<<j<<" "<<k<<endl;
}
cout<<sum<<endl;
for(k=;k<=sum;k++)
cout<<cun[k][]<<" "<<cun[k][]<<endl;
return ;
}
Colored Boots题解的更多相关文章
- D. Colored Boots(STL)
There are nn left boots and nn right boots. Each boot has a color which is denoted as a lowercase La ...
- CodeChef-LECOINS Little Elephant and Colored Coins 题解
CodeChef-LECOINS Little Elephant and Colored Coins Little Elephant and Colored Coins The Little Elep ...
- [CF780C]Andryusha and Colored Balloons 题解
前言 完了,完了,咕值要没了,赶紧写题解QAQ. 题意简述 给相邻的三个节点颜色不能相同的树染色所需的最小颜色数. 题解 这道题目很显然可以用深搜. 考虑题目的限制,如果当前搜索到的点为u, 显然u的 ...
- CF1141D Colored Boots
There are n left boots and n right boots. Each boot has a color which is denoted as a lowercase Lati ...
- Codeforces Round #547 (Div. 3) D. Colored Boots (贪心,模拟)
题意:有两个字符串,两个字符串中的相同字符可以相互匹配,\(?\)可以和任意字符匹配,输出最大匹配的字符数量和它们分别两个字符串中的位置. 题解:很容易贪心,我们先遍历第一个字符串,然后在第二个字符串 ...
- Codeforces Round #547 (Div. 3) D. Colored Boots
链接:https://codeforces.com/contest/1141/problem/D 题意: 给连个n长度的字符串. 求两个字符串相同字符对应位置的对数,并挨个打印. 字符:?可以代替任何 ...
- Codeforces Round #547 (Div. 3) 题解
Codeforces Round #547 (Div. 3) 题目链接:https://codeforces.com/contest/1141 A,B咕咕了... C. Polycarp Restor ...
- Codeforces Round #309 (Div. 2) C. Kyoya and Colored Balls 排列组合
C. Kyoya and Colored Balls Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...
- POJ 3279(Fliptile)题解
以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定长宽的黑白棋棋盘摆满棋子,每次操作可以反转一个位置和其上下左右共五个位置的棋子的颜色,求要使用最少翻转次数将所有棋子反转为黑 ...
随机推荐
- lianxi
package dududu; public class qiqiqi { public static void main(String[] args) { // TODO 自动生成的方法存根 ; ; ...
- Python爬虫教程-25-数据提取-BeautifulSoup4(三)
Python爬虫教程-25-数据提取-BeautifulSoup4(三) 本篇介绍 BeautifulSoup 中的 css 选择器 css 选择器 使用 soup.select 返回一个列表 通过标 ...
- addEventListener(event, function, useCapture) 简记
监听事件方法:addEventListener(<event-name>, <callback>, <use-capture>) 移除监听事件方法:removeEv ...
- Appium 常用方法
锁定 锁定屏幕 # python driver.lock(5) 将 app 置于后台 把当前应用放到后台去 # python driver.background_app(5) 收起键盘 收起键盘 # ...
- Vs2013 & .net framework 4.5.1 预览介绍
微软发布了vs2013 preview 和fw4.5.1 下面简单介绍一下与大家共享 Developer productivity X64 edit and continue 在2013里面 可以在x ...
- Linux 虚拟机中配置 GNOME + VNC
需求描述 在特定的需求下,需要用到 Linux 的图形化界面,但是 Azure 平台提供的虚拟机默认没有开放远程图形化登陆的功能.以下解决方案,提供了市面上非常流行的 GNOME + VNC 的组合来 ...
- 求n的元素的最大最小值
public static int[] maxMin(int a[]) { int[] res = new int[2]; int len = a.length; if (len <= 0) { ...
- ubuntu 安装nodejs和git
1.安装curl sudo apt-get install curl 2.安装nodejs 和 npm curl -sL https://deb.nodesource.com/setup_8.x | ...
- MYSQL 升序排序但值为0的排最后
转载 http://blog.csdn.net/looksun/article/details/51445205 如一张表的数据如下: 需要根据gz列的值进行升序排序,但值为0的排在最后面,即最终结果 ...
- 保存头像- vue项目-base64字符串转图片
<img :onerror="errpic" class="customerHead" :src="param.customerHead&quo ...