个人心得:其实本来这题是有规律的不过当时已经将整个模拟过程都构思出来了,就打算试试,将每个字符和总和用优先队列

装起来,然后枚举每个点,同时进行位置标志,此时需要多少个点的时候拿出最大的和出来,若不满足就输出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

palindrome is a string that reads the same forward and backward. For example, aaaabba and abcba are all palindromes, while ababab 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
a11a12a1W
:
aH1aH2aHW

Output

If Snuke can create a matrix satisfying the condition, print Yes; otherwise, print No.


Sample Input 1

Copy
3 4
aabb
aabb
aacc

Sample Output 1

Copy
Yes

For example, the following matrix satisfies the condition.

abba
acca
abba

Sample Input 2

Copy
2 2
aa
bb

Sample Output 2

Copy
No

It is not possible to create a matrix satisfying the condition, no matter how we rearrange the elements in A.


Sample Input 3

Copy
5 1
t
w
e
e
t

Sample Output 3

Copy
Yes

For example, the following matrix satisfies the condition.

t
e
w
e
t

Sample Input 4

Copy
2 5
abxba
abyba

Sample Output 4

Copy
No

Sample Input 5

Copy
1 1
z

Sample Output 5

Copy
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(模拟所有情况,注意细节)的更多相关文章

  1. [Code Festival 2017 qual A] C: Palindromic Matrix

    题意 给出一个小写字母组成的字符矩阵,问能否通过重排其中的字符使得每行每列都是回文串. 分析 简化版:给出一个字符串,问能否通过重排其中的字符使得它是回文串.那么如果字符串长度为偶数,就需要a到z的个 ...

  2. CODE FESTIVAL 2017 qual A C Palindromic Matrix(补题)

    彩笔看到题目后,除了懵逼,没有啥反应了,唯一想的就是 这是不是dp啊?看了题解才发现,原来是这样啊. 画几个矩阵看看就能看出来规律. 思路:先假设这是个M * N的矩阵 如果M和N都是偶数,则每个出现 ...

  3. CODE FESTIVAL 2017 qual B B - Problem Set【水题,stl map】

    CODE FESTIVAL 2017 qual B B - Problem Set 确实水题,但当时没想到map,用sort后逐个比较解决的,感觉麻烦些,虽然效率高很多.map确实好写点. 用map: ...

  4. CODE FESTIVAL 2017 qual B C - 3 Steps【二分图】

    CODE FESTIVAL 2017 qual B C - 3 Steps 题意:给定一个n个结点m条边的无向图,若两点间走三步可以到,那么两点间可以直接连一条边,已经有边的不能连,问一共最多能连多少 ...

  5. 【AtCoder】CODE FESTIVAL 2017 qual A

    A - Snuke's favorite YAKINIKU -- #include <bits/stdc++.h> #define fi first #define se second # ...

  6. CODE FESTIVAL 2017 qual A 题解

    补一发A的题解. A - Snuke's favorite YAKINIKU 题意: 输入字符串S,如果以YAKI开头输出Yes,否则输出No. #include<bits/stdc++.h&g ...

  7. CODE FESTIVAL 2017 qual B

    昨晚因为有点事就去忙了,没打后悔啊 A - XXFESTIVAL Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem ...

  8. 【题解】Popping Balls AtCoder Code Festival 2017 qual B E 组合计数

    蒟蒻__stdcall终于更新博客辣~ 一下午+一晚上=一道计数题QAQ 为什么计数题都这么玄学啊QAQ Prelude 题目链接:这里是传送门= ̄ω ̄= 下面我将分几个步骤讲一下这个题的做法,大家不 ...

  9. Atcoder CODE FESTIVAL 2017 qual C D - Yet Another Palindrome Partitioning 回文串划分

    题目链接 题意 给定一个字符串(长度\(\leq 2e5\)),将其划分成尽量少的段,使得每段内重新排列后可以成为一个回文串. 题解 分析 每段内重新排列后是一个回文串\(\rightarrow\)该 ...

随机推荐

  1. openstack ocata版(脚本)计算节点安装

    一.初始化环境: 1.安装软件包: yum -y install centos-release-openstack-ocata yum -y upgrade yum -y install python ...

  2. 深入浅出Node.js(下)

    (五):Node.js的异步实现 专栏的第五篇文章<Node.js的异步实现>.之前介绍了Node.js的事件机制,也许读者对此尚会觉得意犹未尽,因为仅仅只是简单的事件机制,并不能道尽No ...

  3. UI组件之UIImage

    UIImageView:图像视图,用于在应用程序中显示图片 UIImage:是将图片文件转换为程序中的图片对象 UIImageView是UIImage的载体 方法一:用此方法创建图片对象,会将图片ca ...

  4. OpenStack虚拟机创建过程中镜像格式的的变化过程

    Glance用来作为独立的大规模镜像查找服务,当它与Nova和Swift配合使用时,就为OpenStack提供了虚拟机镜像的查找服务,像所有的OpenStack项目一样,遵循以下设计思想: 基于组件的 ...

  5. pagination结合ajax

    function getContent(page,Id){ $.ajax({ type:'get', url:'www.baidu.com', dataType:'jsonp', data:{ }, ...

  6. MySQL备份账号权限

    grant select,show view,lock tables,trigger on confluence.* to 'DBbackup'@'127.0.0.1' identified by ' ...

  7. 谈谈 cci 与 i2c

    cci的名字叫, camera control interface, 他由两部分组成,一是i2c ,而另一个部分是 gpio.也就是说,cci 包含i2c.一般情况下,我们只是用到了i2c 部分,没有 ...

  8. Start and Use the Database Engine Tuning Advisor

    https://docs.microsoft.com/en-us/sql/relational-databases/performance/start-and-use-the-database-eng ...

  9. UVA 12307 Smallest Enclosing Rectangle(旋转卡壳)

    题意:给你一些点,找出两个可以包含所有点的矩形,一个保证矩形面积最小,一个保证矩形周长最小,输出两个最小值 题解:首先根据所有点求一个凸包,再在这个凸包上枚举每条边,作为矩形的一条边(这样可以保证最小 ...

  10. 分析新建的android代码

    1).xml文件中的<?xml version="1.0" encoding="utf-8"?>是每个xml文件开仅有一个的声明xml的代码. 2) ...