1. 程式人生 > >CODE FESTIVAL 2017 qual A--C - Palindromic Matrix(模擬所有情況,註意細節)

CODE FESTIVAL 2017 qual A--C - Palindromic Matrix(模擬所有情況,註意細節)

需要 tip stream pac pos ear war sample element

個人心得:其實本來這題是有規律的不過當時已經將整個模擬過程都構思出來了,就打算試試,將每個字符和總和用優先隊列

裝起來,然後枚舉每個點,同時進行位置標誌,此時需要多少個點的時候拿出最大的和出來,若不滿足就輸出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
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 }



CODE FESTIVAL 2017 qual A--C - Palindromic Matrix(模擬所有情況,註意細節)