题目:

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

思路:

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

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

 #include <bits/stdc++.h>

 using namespace std;

 #define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=1e6+;
const int mod=1e9+; char ss[][];
int n,m;
string sa[],sb[],ans;
bool cmp(const string &ta,const string &tb)
{
return ta>tb;
}
//ff为真表示最小,为假表示最大
//S串应该为原串复制两次后的字符串
int mx_mi_express(string &S,bool ff,int len,int lb)
{
int i=,j=,k;
while(i<lb&&j<lb)
{
k=;
while(k<len&&S[i+k]==S[j+k]) k++;
if(k==len) return i<=j?i:j;
if((ff&&S[i+k]>S[j+k]) || (!ff&&S[i+k]<S[j+k]))
{
if(i+k+>j) i=i+k+;
else i=j+;
}
else if((ff&&S[i+k]<S[j+k]) || (!ff&&S[i+k]>S[j+k]))
{
if(j+k+>i) j=j+k+;
else j=i+;
}
}
return i<=j?i:j;
}
int main(void)
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
scanf("%s",&ss[i][]);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
sa[i]+=ss[i][j];
for(int j=;j<=m;j++)
for(int i=;i<=n;i++)
sb[j]+=ss[i][j];
sort(sa+,sa++n,cmp);
sort(sb+,sb++m,cmp);
string ta,tb;
for(int i=;i<=n;i++)
{
ta=sa[i],tb.clear();
for(int j=;j<=n;j++)
if(i!=j) ta+=sa[j];
ta+=ta;
int st=mx_mi_express(ta,,ta.size()/,m);
for(int j=;j<n*m;j++)
tb+=ta[j+st];
ans=max(ans,tb);
}
for(int i=;i<=m;i++)
{
ta=sb[i],tb.clear();
for(int j=;j<=m;j++)
if(i!=j) ta+=sb[j];
ta+=ta;
int st=mx_mi_express(ta,,ta.size()/,n);
for(int j=;j<n*m;j++)
tb+=ta[j+st];
ans=max(ans,tb);
}
int ff=;
for(int i=;i<ans.size();i++)
if(!ff&&ans[i]=='') ;
else printf("%c",ans[i]),ff=;
if(!ff) printf("0\n");
return ;
}

XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem F. Matrix Game的更多相关文章

  1. XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem J. Terminal

    题目:Problem J. TerminalInput file: standard inputOutput file: standard inputTime limit: 2 secondsMemo ...

  2. XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem L. Canonical duel

    题目:Problem L. Canonical duelInput file: standard inputOutput file: standard outputTime limit: 2 seco ...

  3. XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem A. Arithmetic Derivative

    题目:Problem A. Arithmetic DerivativeInput file: standard inputOutput file: standard inputTime limit: ...

  4. 【二分】【字符串哈希】【二分图最大匹配】【最大流】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem I. Minimum Prefix

    给你n个字符串,问你最小的长度的前缀,使得每个字符串任意循环滑动之后,这些前缀都两两不同. 二分答案mid之后,将每个字符串长度为mid的循环子串都哈希出来,相当于对每个字符串,找一个与其他字符串所选 ...

  5. XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem D. Clones and Treasures

    题目:Problem D. Clones and TreasuresInput file: standard inputOutput file: standard outputTime limit: ...

  6. 【二分图】【并查集】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem L. Canonical duel

    给你一个网格(n<=2000,m<=2000),有一些炸弹,你可以选择一个空的位置,再放一个炸弹并将其引爆,一个炸弹爆炸后,其所在行和列的所有炸弹都会爆炸,连锁反应. 问你所能引爆的最多炸 ...

  7. 【动态规划】【滚动数组】【bitset】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem J. Terminal

    有两辆车,容量都为K,有n(10w)个人被划分成m(2k)组,依次上车,每个人上车花一秒.每一组的人都要上同一辆车,一辆车的等待时间是其停留时间*其载的人数,问最小的两辆车的总等待时间. 是f(i,j ...

  8. 【枚举】【最小表示法】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem F. Matrix Game

    给你一个n*m的字符矩阵,将横向(或纵向)全部裂开,然后以任意顺序首尾相接,然后再从中间任意位置切开,问你能构成的字典序最大的字符串. 以横向切开为例,纵向类似. 将所有横排从大到小排序,枚举最后切开 ...

  9. 【推导】【构造】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem E. Space Tourists

    给你n,K,问你要选出最少几个长度为2的K进制数,才能让所有的n位K进制数删除n-2个元素后,所剩余的长度为2的子序列至少有一个是你所选定的. 如果n>K,那么根据抽屉原理,对于所有n位K进制数 ...

随机推荐

  1. RabbitMQ之Queues-5

    工作队列的主要任务是:避免立刻执行资源密集型任务,然后必须等待其完成.相反地,我们进行任务调度:我们把任务封装为消息发送给队列.工作进行在后台运行并不断的从队列中取出任务然后执行.当你运行了多个工作进 ...

  2. Maven项目POM.xml详解

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  3. CSS 属性的默认值

    最近在看到一篇关于如何实现水平垂直居中,发现有许多属性值,自己并不了解,特此Google一番,查到,摘抄过来,方便以后查阅,下面是如何实现水平垂直居中的博文. 解读CSS布局之-水平垂直居中 html ...

  4. [转]ASP.NET MVC 5 - 视图

    在本节中,你要去修改HelloWorldController类,使用视图模板文件,在干净利索地封装的过程中:客户端浏览器生成HTML. 您将创建一个视图模板文件,其中使用了ASP.NET MVC 3所 ...

  5. easyui上传文件

    效果图: 代码: <form id="importFileForm" method="post" enctype="multipart/form ...

  6. shell脚本学习总结04--终端信息的获取和设置

    tput tput 命令将通过 terminfo 数据库对您的终端会话进行初始化和操作.通过使用 tput,您可以更改几项终端功能,如移动或更改光标.更改文本属性,以及清除终端屏幕的特定区域. 文本属 ...

  7. NSUserDefaults设置bool值重启后bool只设置丢失问题

    本文转载至 http://blog.csdn.net/cerastes/article/details/38036875   NSUserDefaultsbool同步synchronize无效 今天使 ...

  8. SaltStack自动化安装zabbix-server

    使用SaltStack自动化安装zabbix-server 1,设置ntp时间同步 2,安装zabbix-agent 3,安装zabbix-server 4,安装及配置mariadb(mariadb与 ...

  9. 【python】-- RabbitMQ 队列消息持久化、消息公平分发

    RabbitMQ 队列消息持久化 假如消息队列test里面还有消息等待消费者(consumers)去接收,但是这个时候服务器端宕机了,这个时候消息是否还在? 1.队列消息非持久化 服务端(produc ...

  10. node.js + mongodb 做项目的详解(二)

    这次内容是结合bootstrap把登陆注册做好,还有就是express的中间件等问题. 看这篇博客之前建议先看我上篇写的那篇博客http://www.cnblogs.com/hubwiz/p/4118 ...