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

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

    1.下载软件包 yum install nfs-utils nfs-utils-lib -y 2.编辑/etc/exports文件: 1.创建目录:mkdir -p /home/glance2.编辑e ...

  2. Fidder详解之抓包

    前言 本文是博主发表的第一篇文章,如有傻逼之处,请大家见谅.最近遇到很多人说接口相关的问题,比如:什么是接口,我该怎么做接口测试,还有我总是抓不到APP上的https请求(这个巨坑,不知道坑了多少小白 ...

  3. iOS 52个技巧学习心得笔记 第一章 熟悉OC

    1 .简单了解OC2 .在类的头文件中尽量少引入其他头文件3 .多用字面量语法 少用与之等价的方法 4 .多用类型常量 少用 #define 预处理指令5 .用枚举表示状态,选项,状态码 .简单了解O ...

  4. $Java设计模式之——观察者模式(Observer)

    (一)观察者模式简介 1.定义:定义对象间一种一对多的依赖关系,一个对象状态发生改变时,所有依赖它的对象都会接到通知并作出相应的响应. 2.应用场景: (1)GUI系统 (2)订阅-发布系统 (3)事 ...

  5. Python自然语言处理系列之模拟退火算法

    1.基本概念 模拟退火算法(Simulated Annealing,SA)是一种模拟固体降温过程的最优化算法.其模拟的过程是首先将固体加温至某一温度,固体内部的粒子随温度上升慢慢变为无序的状态,内能增 ...

  6. LVS 介绍

    LVS 介绍 说明: LVS是Linux Virtual Server的简称 LVS是一个实现负载均衡的开源软件项目 LVS效率要高于Nginx LVS工作在ISO的第4层(传输层) LVS架构有三层 ...

  7. Pandas基础用法-数据处理【全】-转

    完整资料:[数据挖掘入门介绍] (https://github.com/YouChouNoBB/data-mining-introduction) # coding=utf-8 # @author: ...

  8. 【简单dp】poj 2127 Greatest Common Increasing Subsequence【最长公共上升子序列】【模板】

    Sample Input 5 1 4 2 5 -12 4 -12 1 2 4 Sample Output 2 1 4 题目:给你两个数字序列,求出这两个序列的最长公共上升子序列.输出最长的长度,并打表 ...

  9. MYSQL数据库字段命名及设计规范

    1.设计原则 1) 标准化和规范化数据的标准化有助于消除数据库中的数据冗余.标准化有好几种形式,但 Third Normal Form(3NF)通常被认为在性能.扩展性和数据完整性方面达到了最好平衡. ...

  10. 【P1379】八数码难题(搜索+暴力)

    这个题真是... 不想说什么了,及其复杂和烦人的一道题.基础思路就是bfs,用两个队列分别进行0的位置的计算和每一步的状态..然而这个题最重要的一点在于判重,实际上可以康托展开用全排列的个数进行判重, ...