标签:following 意思 tip code ret int stack app case
个人心得:其实本来这题是有规律的不过当时已经将整个模拟过程都构思出来了,就打算试试,将每个字符和总和用优先队列
装起来,然后枚举每个点,同时进行位置标志,此时需要多少个点的时候拿出最大的和出来,若不满足就输出No,结果一直卡在三组
数据。比赛完后一想,优先队列虽然用处大,不过当行列存在奇数的时候此时只要2个就可以,而如果你从最大的4个中拿出来,
那么下一层循环中必然有一个位置无法填充,如此就导致了算法的失败。所以后面建立个奇偶判定就好了。
感悟:
多注意思考和细节,从不同的层次看待问题,既可以全面又可以优化所有细节!
题目:
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:
Determine whether he can create a matrix satisfying the condition.
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.
Input is given from Standard Input in the following format:
H W a11a12…a1W : aH1aH2…aHW
If Snuke can create a matrix satisfying the condition, print Yes
; otherwise, print No
.
3 4 aabb aabb aacc
Yes
For example, the following matrix satisfies the condition.
abba acca abba
2 2 aa bb
No
It is not possible to create a matrix satisfying the condition, no matter how we rearrange the elements in A.
5 1 t w e e t
Yes
For example, the following matrix satisfies the condition.
t e w e t
2 5 abxba abyba
No
1 1 z
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(模拟所有情况,注意细节)
标签:following 意思 tip code ret int stack app case
原文地址:http://www.cnblogs.com/blvt/p/7586499.html