题目:

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. hdu 1233:还是畅通工程(数据结构,图,最小生成树,普里姆(Prim)算法)

    还是畅通工程 Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  2. 【Debian】ftp安装

    http://www.2cto.com/os/201107/98311.html http://jingyan.baidu.com/article/adc815133476bdf723bf7393.h ...

  3. 说说M451例程之PWM

    /**************************************************************************//** * @file main.c * @ve ...

  4. Docker1.12 + Swarm 构建动态微服务应用

    导读 我们在之前提到过一个示例,即一款由前端与多项后端服务共同构成的微服务应用.其中前端为Traefik HTTP代理,负责将各项请求路由至后端服务.而后端则非常简单,是一套基于Go的HTTP Web ...

  5. 【BZOJ4244】邮戳拉力赛 DP

    [BZOJ4244]邮戳拉力赛 Description IOI铁路是由N+2个站点构成的直线线路.这条线路的车站从某一端的车站开始顺次标号为0...N+1. 这条路线上行驶的电车分为上行电车和下行电车 ...

  6. [Android Tips] 29. 如何判断当前编译的是哪个 Flavor ?

    背景说明 应用需要针对不同的市场集成不同的第三方 SDK ,比如:面向海市场的版本需要集成 google-service apply plugin: 'com.google.gms.google-se ...

  7. 160525、高并发之mysql主从复制(linux)

    第一步:新建两台linux主机(我这里是使用虚拟机,linux版本是CentOS-6.3-x86_64-bin-DVD1.iso,注意:我下载的是dvd1,如果其他版本默认安装可能会自动还原) 第二步 ...

  8. Zabbix监控Windows主机

    一,下载zabbix-agent 下载地址:http://www.zabbix.com/downloads/3.0.0/zabbix_agents_3.0.0.win.zip 已经下载好的文件 zab ...

  9. Code Forces 645A Amity Assessment

    A. Amity Assessment time limit per test2 seconds memory limit per test256 megabytes inputstandard in ...

  10. SignalR 循序渐进(四) Hub的生命周期以及IoC

    有阵子没更新这个系列了,最近太忙了.本篇带来的是Hub的生命周期以及IoC. 首先,Hub的生命周期,我们用一个Demo来看看: public class TestHub : Hub { public ...