CODE FESTIVAL 2017 qual A--C - Palindromic Matrix(模拟所有情况,注意细节)
个人心得:其实本来这题是有规律的不过当时已经将整个模拟过程都构思出来了,就打算试试,将每个字符和总和用优先队列
装起来,然后枚举每个点,同时进行位置标志,此时需要多少个点的时候拿出最大的和出来,若不满足就输出No,结果一直卡在三组
数据。比赛完后一想,优先队列虽然用处大,不过当行列存在奇数的时候此时只要2个就可以,而如果你从最大的4个中拿出来,
那么下一层循环中必然有一个位置无法填充,如此就导致了算法的失败。所以后面建立个奇偶判定就好了。
感悟:
多注意思考和细节,从不同的层次看待问题,既可以全面又可以优化所有细节!
题目:
Problem Statement
We have an H-by-W matrix. Let aij be the element at the i-th row from the top and j-th column from the left. In this matrix, each aij is a lowercase English letter.
Snuke is creating another H-by-W matrix, A', by freely rearranging the elements in A. Here, he wants to satisfy the following condition:
- Every row and column in A' can be read as a palindrome.
Determine whether he can create a matrix satisfying the condition.
Note
A palindrome is a string that reads the same forward and backward. For example, a
, aa
, abba
and abcba
are all palindromes, while ab
, abab
andabcda
are not.
Constraints
- 1≤H,W≤100
- aij is a lowercase English letter.
Input
Input is given from Standard Input in the following format:
H W
a11a12…a1W
:
aH1aH2…aHW
Output
If Snuke can create a matrix satisfying the condition, print Yes
; otherwise, print No
.
Sample Input 1
3 4
aabb
aabb
aacc
Sample Output 1
Yes
For example, the following matrix satisfies the condition.
abba
acca
abba
Sample Input 2
2 2
aa
bb
Sample Output 2
No
It is not possible to create a matrix satisfying the condition, no matter how we rearrange the elements in A.
Sample Input 3
5 1
t
w
e
e
t
Sample Output 3
Yes
For example, the following matrix satisfies the condition.
t
e
w
e
t
Sample Input 4
2 5
abxba
abyba
Sample Output 4
No
Sample Input 5
1 1
z
Sample Output 5
Yes
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<stack>
#include<set>
#include<queue>
#include<algorithm>
using namespace std;
#define in 1000000007
int h,w;
char ch[][];
int ok=;
struct mapa
{
char x;
int sum;
mapa(char m,int n)
{
x=m;
sum=n;
}
bool operator <(const mapa &a)const
{
return sum<a.sum;
}
};
int sum[];
priority_queue<mapa >pq;
int book[][];
void flag(int i,int j){
book[i][j]=;
int t=;
if(!book[i][w--j])
{
t++;
book[i][w--j]=;
}
if(!book[h--i][j])
{
t++;
book[h--i][j]=;
}
if(!book[h--i][w--j])
{
t++;
book[h--i][w--j]=;
}
mapa a=pq.top();pq.pop();
if(a.sum<t)
{
ok=;
return false;
}
a.sum=a.sum-t;
if(a.sum)
pq.push(a);
}
bool dfs()
{
memset(book,,sizeof(book));
int p=,q=;
int i,j;
if(h%==) p=;
if(w%==) q=;
for(i=;i<h;i++){
if(p&&i==h/) break;
for(j=;j<w;j++)
{
if(q&&j==w/) break;
if(book[i][j]) continue;
else
{
flag(i,j);
}
}
}
if(p)
{
for(int k=;k<w;k++)
{
if(q&&k==w/) break;
if(book[i][k]) continue;
book[i][k]=;
int t=;
if(!book[i][w--k])
{
t++;
book[i][w--k]=;
}
mapa a=pq.top();pq.pop();
if(a.sum<t)
{
ok=;
return false;
}
a.sum=a.sum-t;
if(a.sum)
pq.push(a);
}
}
if(q)
{
for(int k=;k<h;k++)
{
if(book[k][j]) continue;
book[k][j]=;
int t=;
if(!book[h--k][j])
{
t++;
book[h--k][j]=;
}
mapa a=pq.top();pq.pop();
if(a.sum<t)
{
ok=;
return false;
}
a.sum=a.sum-t;
if(a.sum)
pq.push(a);
}
}
return true;
}
int main()
{
cin>>h>>w;
for(int i=;i<h;i++)
for(int j=;j<w;j++)
cin>>ch[i][j];
memset(sum,,sizeof(sum));
for(int i=;i<h;i++)
for(int j=;j<w;j++)
sum[ch[i][j]-'a']++;
for(int i=;i<;i++)
if(sum[i])
{
char t=i+'a';
pq.push(mapa(t,sum[i]));
}
if(h==||w==)
{
int flag=;
for(int i=;i<;i++)
if(sum[i]%!=) flag++;
if(flag>) cout<<"No"<<endl;
else
cout<<"Yes"<<endl;
}
else{
if(dfs())
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
} return ;
}
CODE FESTIVAL 2017 qual A--C - Palindromic Matrix(模拟所有情况,注意细节)的更多相关文章
- [Code Festival 2017 qual A] C: Palindromic Matrix
题意 给出一个小写字母组成的字符矩阵,问能否通过重排其中的字符使得每行每列都是回文串. 分析 简化版:给出一个字符串,问能否通过重排其中的字符使得它是回文串.那么如果字符串长度为偶数,就需要a到z的个 ...
- CODE FESTIVAL 2017 qual A C Palindromic Matrix(补题)
彩笔看到题目后,除了懵逼,没有啥反应了,唯一想的就是 这是不是dp啊?看了题解才发现,原来是这样啊. 画几个矩阵看看就能看出来规律. 思路:先假设这是个M * N的矩阵 如果M和N都是偶数,则每个出现 ...
- CODE FESTIVAL 2017 qual B B - Problem Set【水题,stl map】
CODE FESTIVAL 2017 qual B B - Problem Set 确实水题,但当时没想到map,用sort后逐个比较解决的,感觉麻烦些,虽然效率高很多.map确实好写点. 用map: ...
- CODE FESTIVAL 2017 qual B C - 3 Steps【二分图】
CODE FESTIVAL 2017 qual B C - 3 Steps 题意:给定一个n个结点m条边的无向图,若两点间走三步可以到,那么两点间可以直接连一条边,已经有边的不能连,问一共最多能连多少 ...
- 【AtCoder】CODE FESTIVAL 2017 qual A
A - Snuke's favorite YAKINIKU -- #include <bits/stdc++.h> #define fi first #define se second # ...
- CODE FESTIVAL 2017 qual A 题解
补一发A的题解. A - Snuke's favorite YAKINIKU 题意: 输入字符串S,如果以YAKI开头输出Yes,否则输出No. #include<bits/stdc++.h&g ...
- CODE FESTIVAL 2017 qual B
昨晚因为有点事就去忙了,没打后悔啊 A - XXFESTIVAL Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem ...
- 【题解】Popping Balls AtCoder Code Festival 2017 qual B E 组合计数
蒟蒻__stdcall终于更新博客辣~ 一下午+一晚上=一道计数题QAQ 为什么计数题都这么玄学啊QAQ Prelude 题目链接:这里是传送门= ̄ω ̄= 下面我将分几个步骤讲一下这个题的做法,大家不 ...
- Atcoder CODE FESTIVAL 2017 qual C D - Yet Another Palindrome Partitioning 回文串划分
题目链接 题意 给定一个字符串(长度\(\leq 2e5\)),将其划分成尽量少的段,使得每段内重新排列后可以成为一个回文串. 题解 分析 每段内重新排列后是一个回文串\(\rightarrow\)该 ...
随机推荐
- JavaScript:学习笔记(1)——在HTML中使用JS
在HTML中使用JavaScript <script>元素 1.直接在网页中嵌入JS代码 说明: 请不要在代码的任何地方出现</script>字符串 这是由于解析嵌入式代码的规 ...
- CENTOS 搭建SVN服务器(附自动部署到远程WEB)
安装subversion服务端 # 安装 yum install -y subversion # 测试是否安装成功 如果显示了版本信息则表示安装成功 svnserve --version;sleep ...
- PAT 天梯赛 L1-023. 输出GPLT 【水】
题目链接 https://www.patest.cn/contests/gplt/L1-023 AC代码 #include <iostream> #include <cstdio&g ...
- 微服务架构~BFF和网关是如何演化出来的
介绍 BFF(Backend for Frontend)和网关Gateway是微服务架构中的两个重要概念,这两个概念相对比较新,有些开发人员甚至是架构师都不甚理解. 本文用假想的公司案例+图示的方式, ...
- $MySQL常用操作命令备忘
1.登录数据库:mysql -uroot -p12345 (12345为密码)2.创建数据库:create database senguocc; (senguocc为数据库名)3.查看有哪些数据库: ...
- STM32 USB虚拟串口
串口调试在项目中被使用越来越多,串口资源的紧缺也变的尤为突出.很多本本人群,更是深有体会,不准备一个USB转串口工具就没办法进行开发.本章节来简单概述STM32低端芯片上的USB虚拟串口的移植.在官方 ...
- Docker容器技术-镜像分发
一.镜像分发 1.镜像及镜像库的命名方式 指定镜像名称和标签的方法: 在狗偶见镜像时 通过docker tag命令 [root@bogon ~]# cd identidock/ [root@bogon ...
- JZ2440专用dnw 支持xp、win7、win8和win10系统【转】
本文转载自:https://blog.csdn.net/czg13548930186/article/details/76999152 学习于韦东山百问网公司 本文用于解决win7以上系统使用dnw难 ...
- DB2日期及时间的使用
1.DB2中日期.时间寄存器的使用 --取日期 SELECT CURRENT DATE FROM sysibm.sysdummy1; --返回数据类型:DATE --结果集(YYYY-MM-DD): ...
- 在Java项目中部署使用Memcached[转]
在项目中使用到Memcached主要的目的是,通过缓存数据库查询结果,减少数据库访问次数,从而提高动态.数据库驱动网站的速度.提高可扩展性.Memcached是一个高性能的分布式内存对象缓存系统,基于 ...