您的位置:首页 > Web前端

CODE FESTIVAL 2017 qual A C

2017-09-25 11:24 597 查看
C - Palindromic Matrix

Time limit : 2sec / Memory limit : 256MB

Score : 400 points

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 and abcda 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

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 <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;

char str[111][111];
int a[30];

int main(){
int h,w;
scanf("%d %d",&h,&w);
for(int i=0;i<h;i++){
scanf("%s",str[i]);
for(int j=0;j<w;j++){
a[str[i][j]-'a'+1]++;
}
}
int g1=(h%2)*(w%2);
int g2=(h/2)*(w%2)+(w/2)*(h%2);
int g3=(h/2)*(w/2);
for(int i=1;i<=26;i++){
g3-=(a[i]/4);
a[i]%=4;
g2-=(a[i]/2);
a[i]%=2;
g1-=a[i];
}
g2+=(g3*2);
if(g3>0||g2>0||g1>0){
puts("No");
return 0;
}
puts("Yes");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: