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

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
  1 #include<iostream>
  2 #include<cstring>
  3 #include<string>
  4 #include<cstdio>
  5 #include<vector>
  6 #include<cmath>
  7 #include<stack>
  8 #include<set>
  9 #include<queue>
 10 #include<algorithm>
 11 using namespace std;
 12 #define in 1000000007
 13 int h,w;
 14 char ch[105][105];
 15 int ok=0;
 16 struct mapa
 17 {
 18     char x;
 19     int sum;
 20     mapa(char m,int n)
 21     {
 22         x=m;
 23         sum=n;
 24     }
 25     bool operator <(const mapa &a)const
 26     {
 27         return sum<a.sum;
 28     }
 29 };
 30 int sum[27];
 31 priority_queue<mapa >pq;
 32 int book[105][105];
 33 void flag(int i,int j){
 34       book[i][j]=1;
 35             int t=1;
 36             if(!book[i][w-1-j])
 37             {
 38                 t++;
 39                 book[i][w-1-j]=1;
 40             }
 41             if(!book[h-1-i][j])
 42             {
 43                 t++;
 44                 book[h-1-i][j]=1;
 45             }
 46             if(!book[h-1-i][w-1-j])
 47             {
 48                 t++;
 49                 book[h-1-i][w-1-j]=1;
 50             }
 51            mapa a=pq.top();pq.pop();
 52            if(a.sum<t)
 53            {
 54                ok=1;
 55                return false;
 56            }
 57            a.sum=a.sum-t;
 58            if(a.sum)
 59               pq.push(a);
 60 }
 61 bool dfs()
 62 {
 63     memset(book,0,sizeof(book));
 64     int p=0,q=0;
 65     int i,j;
 66     if(h%2==1) p=1;
 67     if(w%2==1) q=1;
 68     for(i=0;i<h;i++){
 69         if(p&&i==h/2) break;
 70        for(j=0;j<w;j++)
 71        {
 72            if(q&&j==w/2) break;
 73         if(book[i][j]) continue;
 74         else
 75         {
 76            flag(i,j);
 77         }
 78        }
 79        }
 80        if(p)
 81        {
 82            for(int k=0;k<w;k++)
 83            {
 84                if(q&&k==w/2) break;
 85                if(book[i][k]) continue;
 86                book[i][k]=1;
 87             int t=1;
 88             if(!book[i][w-1-k])
 89             {
 90                 t++;
 91                 book[i][w-1-k]=1;
 92             }
 93            mapa a=pq.top();pq.pop();
 94            if(a.sum<t)
 95            {
 96                ok=1;
 97                return false;
 98            }
 99            a.sum=a.sum-t;
100            if(a.sum)
101               pq.push(a);
102         }
103        }
104        if(q)
105        {
106            for(int k=0;k<h;k++)
107            {
108                if(book[k][j]) continue;
109                book[k][j]=1;
110             int t=1;
111             if(!book[h-1-k][j])
112             {
113                 t++;
114                 book[h-1-k][j]=1;
115             }
116            mapa a=pq.top();pq.pop();
117            if(a.sum<t)
118            {
119                ok=1;
120                return false;
121            }
122            a.sum=a.sum-t;
123            if(a.sum)
124               pq.push(a);
125         }
126        }
127        return true;
128 }
129 int main()
130 {
131     cin>>h>>w;
132     for(int i=0;i<h;i++)
133          for(int j=0;j<w;j++)
134              cin>>ch[i][j];
135              memset(sum,0,sizeof(sum));
136     for(int i=0;i<h;i++)
137          for(int j=0;j<w;j++)
138             sum[ch[i][j]-‘a‘]++;
139         for(int i=0;i<27;i++)
140             if(sum[i])
141             {
142                 char t=i+‘a‘;
143                 pq.push(mapa(t,sum[i]));
144             }
145             if(h==1||w==1)
146         {
147             int flag=0;
148             for(int i=0;i<27;i++)
149                 if(sum[i]%2!=0) flag++;
150             if(flag>1) cout<<"No"<<endl;
151             else
152                 cout<<"Yes"<<endl;
153         }
154         else{
155             if(dfs())
156                cout<<"Yes"<<endl;
157                else
158                  cout<<"No"<<endl;
159         }
160
161     return 0;
162 }
				
时间: 2024-09-28 23:39:20

CODE FESTIVAL 2017 qual A--C - Palindromic Matrix(模拟所有情况,注意细节)的相关文章

CODE FESTIVAL 2017 qual A C Palindromic Matrix(思维题)

题目链接:点我呀 题意:给出n*m由26位小写字母组成的矩阵,问是否能够重构形成一个每行每列都是回文的矩阵 题解:分三种情况考虑(偶偶,奇奇,奇偶),每种情况下考虑最少 需要4个相同字母的字母数,2个相同字母的字母数和一个字母的字母数(只有奇奇的时候才需要一个字母) 最后判断一下.感觉自己的这个代码不是很简洁,明天起来看看别人怎么写的,睡觉去... 1 //Atcoder 3 2 #include <cstring> 3 #include <iostream> 4 #include

[Atcoder Code Festival 2017 Qual B Problem F]Largest Smallest Cyclic Shift

题目大意:给你\(A\)个a,\(B\)个b,\(C\)个c,要你构造一个字符串,使它的最小循环表示法最大.求这个表示法.解题思路:不知道怎么证,但把a.b.c当做单独的字符串扔进容器,每次把字典序最小的和字典序最大的两个字符串合并就是答案.容器用multiset即可. C++ Code: #include<cstdio> #include<set> #include<string> using namespace std; multiset<string>

CODE FESTIVAL 2017 qual A--B-fLIP(换种想法,暴力枚举)

个人心得:开始拿着题目还是有点懵逼的,以前做过相同的,不过那是按一个位置行列全都反之,当时也是没有深究.现在在打比赛不得不 重新构思,后面一想把所有的状态都找出来,因为每次确定了已经按下的行和列后,按不同的操作所加的数都是一样的,于是就想到了用set 暴力枚举,从1-n个分别行列按钮,然后再枚举不同操作即确定行时再对列进行操作,每次操作放入set就可以了. 题目: Problem Statement We have a grid with N rows and M columns of squa

atcoder CODE FESTIVAL 2017 qual C D - Yet Another Palindrome Partitioning

Problem Statement We have a string s consisting of lowercase English letters. Snuke is partitioning s into some number of non-empty substrings. Let the subtrings obtained be s1, s2, …, sNfrom left to right. (Here, s=s1+s2+…+sN holds.) Snuke wants to

CODE FESTIVAL 2017 qual C F

以上为官方题解..

【Atcoder】CODE FESTIVAL 2017 qual C D - Yet Another Palindrome Partitioning

[题意] 给定只含小写字母的字符串,要求分割成若干段使段内字母重组顺序后能得到回文串,求最少分割段数.n<=2*10^5 [题解] 关键在于快速判断一个字符子串是否合法,容易发现合法仅当不存在或只存在一个奇数字符,其余字符均为偶数. 当涉及到奇偶性(%2)时,很自然能想到异或. 将小写字母a~z转化2^0~2^25,那么一个字符子串合法当且仅当其连续异或值是0或2^i(0<=i<=25). 令f[i]表示前i个合法的最少段数,sum[i]表示异或前缀和,则有: f[i]=min(f[j]

101 to 010 Atcoder CODE FESTIVAL 2017 qual B D

https://www.luogu.org/problemnew/show/AT3575 题解 根本不会.. 错误记录:缺少32行的转移.显然这个转移是必要的 1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #include<vector> 5 using namespace std; 6 #define fi first 7 #define se second 8 #define

【AtCoder】CODE FESTIVAL 2017 qual C

A - Can you get AC? No #include <bits/stdc++.h> #define fi first #define se second #define pii pair<int,int> #define pb push_back #define mp make_pair using namespace std; typedef long long int64; char s[15]; int main() { scanf("%s",

【AtCoder】CODE FESTIVAL 2017 qual B

最近不知道为啥被安利了饥荒,但是不能再玩物丧志了,不能颓了 饥荒真好玩 A - XXFESTIVAL CCFESTIVAL #include <bits/stdc++.h> #define fi first #define se second #define pii pair<int,int> #define mp make_pair #define pb push_back #define space putchar(' ') #define enter putchar('\n'