XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem F. Matrix Game

题目:

Problem F. Matrix Game
Input file: standard input
Output file: standard input
Time limit: 1 second
Memory limit: 256 mebibytes
Alice and Bob are playing the next game. Both have same matrix N × M filled with digits from 0 to 9.
Alice cuts the matrix vertically, choose order of the columns, then links all the columns to each other to
have the cyclic sequence of N × M digits. Note that she cannot rotate the columns, i.e. end of some
column must be linked to beginning of the other column. Then she cuts a sequence and reads the decimal
representation of integer A upside down.
Bob cuts the matrix horizontally, choose order of the rows, then link all the rows to each other to have
the cyclic sequence of N × M digits. Note that he cannot rotate the rows, i.e. end of some row must be
linked to beginning of the other row. Then he cuts a sequence and reads the decimal representation of
integer B from left to right.
Player who obtained the biggest integer wins. If both integers are are equal, then game is tied. You are
given the matrix, find the number obtained by winner (or by both players in case of tie), if both Alice
and Bob are playing optimally.
Input
First line contains integers N and M (1 M; N 100).
Each of next N lines contains string of M digits (without the spaces or other delimiters inside) — the
given matrix. You may assume that atleast one digit in the matrix is not equal to 0.
Output
Print answer to the problem. Note that number must be printed without leading zeroes.
Example

standard input standard input
2 2
28
27
8722


思路:

  贪心的将分隔串按字典序从大到小排序。然后枚举把一个串放到第一个串前面,判断是否产生更优解。

  判断最优解的过程用字符串的最大最小表示法即可。

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 #define MP make_pair
 6 #define PB push_back
 7 typedef long long LL;
 8 typedef pair<int,int> PII;
 9 const double eps=1e-8;
10 const double pi=acos(-1.0);
11 const int K=1e6+7;
12 const int mod=1e9+7;
13 
14 char ss[200][200];
15 int n,m;
16 string sa[200],sb[200],ans;
17 bool cmp(const string &ta,const string &tb)
18 {
19     return ta>tb;
20 }
21 //ff为真表示最小,为假表示最大
22 //S串应该为原串复制两次后的字符串
23 int mx_mi_express(string &S,bool ff,int len,int lb)
24 {
25     int i=0,j=1,k;
26     while(i<lb&&j<lb)
27     {
28         k=0;
29         while(k<len&&S[i+k]==S[j+k]) k++;
30         if(k==len)  return i<=j?i:j;
31         if((ff&&S[i+k]>S[j+k]) || (!ff&&S[i+k]<S[j+k]))
32         {
33             if(i+k+1>j) i=i+k+1;
34             else    i=j+1;
35         }
36         else if((ff&&S[i+k]<S[j+k]) || (!ff&&S[i+k]>S[j+k]))
37         {
38             if(j+k+1>i) j=j+k+1;
39             else    j=i+1;
40         }
41     }
42     return i<=j?i:j;
43 }
44 int main(void)
45 {
46     scanf("%d%d",&n,&m);
47     for(int i=1;i<=n;i++)
48         scanf("%s",&ss[i][1]);
49     for(int i=1;i<=n;i++)
50     for(int j=1;j<=m;j++)
51         sa[i]+=ss[i][j];
52     for(int j=1;j<=m;j++)
53     for(int i=1;i<=n;i++)
54         sb[j]+=ss[i][j];
55     sort(sa+1,sa+1+n,cmp);
56     sort(sb+1,sb+1+m,cmp);
57     string ta,tb;
58     for(int i=1;i<=n;i++)
59     {
60         ta=sa[i],tb.clear();
61         for(int j=1;j<=n;j++)
62         if(i!=j)    ta+=sa[j];
63         ta+=ta;
64         int st=mx_mi_express(ta,0,ta.size()/2,m);
65         for(int j=0;j<n*m;j++)
66             tb+=ta[j+st];
67         ans=max(ans,tb);
68     }
69     for(int i=1;i<=m;i++)
70     {
71         ta=sb[i],tb.clear();
72         for(int j=1;j<=m;j++)
73         if(i!=j)    ta+=sb[j];
74         ta+=ta;
75         int st=mx_mi_express(ta,0,ta.size()/2,n);
76         for(int j=0;j<n*m;j++)
77             tb+=ta[j+st];
78         ans=max(ans,tb);
79     }
80     int ff=0;
81     for(int i=0;i<ans.size();i++)
82     if(!ff&&ans[i]=='0') ;
83     else printf("%c",ans[i]),ff=1;
84     if(!ff) printf("0\n");
85     return 0;
86 }

 

posted @ 2017-08-05 23:58  weeping  阅读(288)  评论(0编辑  收藏  举报