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 ;
}
最新文章
- ABP文档 - 审计日志
- 小菜学习Winform(三)Socket点对点通信
- VS.Net 2015 Update3 学习(1) 支持Webpack
- CHAP认证原理
- Codeigniter整合Tank Auth权限类库的教程
- multiple backgrounds 多重背景
- C#中的ref和out的区别
- Linux下php安装phpredis
- 转:SQL Server 批量插入数据的两种方法
- console调试--转
- 抽出SqlHelper
- java类固定值代替基表写法
- vs打开项目出错:未找到导入的项目“C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\BuildCustomizations\CUDA 5.0.props”的解决办法
- [ios]quartz2d画板功功能实现核心代码
- 一步一步学J2SE-ConcurrentHashMap原理
- Linux的sleep()和usleep()
- git工具——版本的创建与回退
- RestExpress response中addHeader 导致stackOverflow
- 『计算机视觉』Mask-RCNN_推断网络其六:Mask生成
- unity3D开发的程序发布到Android平台上进行运行测试的详细步骤