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\)该 ...
随机推荐
- Docker部署JavaWeb项目实战
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主要讲了如何在Ubuntu14.04 64位系统下来创建一个运行Javaweb应用程 ...
- PsySH——PHP交互式控制台
PsySH PsySH is a runtime developer console, interactive debugger and REPL for PHP. PsySH是一个PHP的运行时开发 ...
- ZOJ 3961 Let's Chat 【水】
题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3961 题意 给出两个人的发消息的记录,然后 如果有两人在连续M天 ...
- 如何修改Eclipse中的快捷键
首先打开Eclipse,Windows->Preferences ↓ 进入Preferences界面后,选择General->Keys ↓ 接下来你就会看到: 接下来点击OK就可以生效了.
- ag-grid
使用: import { AgGridVue } from "ag-grid-vue"; <ag-grid-vue style="width:100%;height ...
- web中的编码问题
response返回有两种,一种是字节流outputstream,一种是字符流printwrite. 先说字节流,要输出“中国",给输出流的必须是转换为utf-8的“中国”,还要告诉浏览器, ...
- 树莓派打造对话机器人 Python(转)
工具列表 1. **树莓派**(型号不要求,本人使用的是3B) 2. **usb麦克风**(某宝有卖,我就不打广告了) 用来录音 3. **音响或者喇叭**(某宝也有卖) 用来播放 以上就是需要的工具 ...
- P4501 [ZJOI2018]胖
题目 P4501 [ZJOI2018]胖 官方口中的送分题 做法 我们通过手玩(脑补),\(a_i\)所作的贡献(能更新的点)为:在\(a_i\)更新\(\forall x\)更新前前没有其他点能把\ ...
- HTML如何禁止文本框输入
禁止文本框输入有以下两种方法: 1,设置input为只读状态,代码如下: <input readonly="readonly" value="test1" ...
- display:inline-block 间隙
IE6/7是不支持display:inline-block属性,只是让其表现的跟inline-block一样,尤其对于inline水平的元素,其表现度可以用perfect一词来形容了. 对于IE8+以 ...