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
思维,数学
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; }
相关文章推荐
- atcoder CODE FESTIVAL 2017 qual A 手速(雾)赛
- Code Festival 2017 qualA D Four Coloring[构造]
- Code Festival 2017 qualA E-Modern Painting
- atcoder/CODE FESTIVAL 2017 qual B/B(dfs染色判断是否为二分图)
- [Atcoder CODE FESTIVAL 2017 qual C]D - Yet Another Palindrome Partitioning 状压DP
- 【AtCoder CODE FESTIVAL 2017 qual C】D - Yet Another Palindrome Partitioning (状压dp 字符串)
- 【题解】Popping Balls AtCoder Code Festival 2017 qual B E 组合计数
- CODE FESTIVAL 2017 qual C- A-B-C 总结
- 找规律 B - Palindrome-phobia(CODE FESTIVAL 2017 Final)
- Atcoder CODE FESTIVAL 2017 qual C D - Yet Another Palindrome Partitioning
- CODE FESTIVAL 2017 qual B:C - 3 Steps 并查集判二分图、二分图性质
- 【二分图染色】AtCoder CODE FESTIVAL 2017(qual B)C[3 Steps]题解
- Atcoder CODE FESTIVAL 2017 Final 简要题解
- [DP]Atcoder CODE FESTIVAL 2017 Final (Parallel) D - Zabuton
- B - Palindrome-phobia(CODE FESTIVAL 2017 Final)
- Atcoder CODE FESTIVAL 2017 qual C 总结+ABCD题解
- Atcoder CODE FESTIVAL 2016 qual C 的E题 Encyclopedia of Permutations
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) E. The Holmes Children
- Codechef 2017 March Challenge 简要题解
- codeforces Technocup 2017 - Elimination Round 2/Codeforces Round #380 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 2) 题解