题目:

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. Mac终端使用mysql

    1.安装完mysql后给mysql命别名 alias mysql=/usr/local/mysql/bin/mysql aliasmysqladmin=/usr/local/mysql/bin/mys ...

  2. 采用std::thread 替换 openmp

    内容转换的,具体详见博客:https://cloud.tencent.com/developer/article/1094617 及对应的code:https://github.com/cpuimag ...

  3. PHP项目中经常用到的无限极分类函数

    //无限极分类函数调用 function getTree($data,$parent_id = 0,$dept_level = 0){ static $tree = []; foreach ($dat ...

  4. iOS文件路径相关的方法

    文件路径相关的方法在NSPathUtilities中,主要是操作路径 获得一个路径 NSString *documents = [NSSearchPathForDirectoriesInDomains ...

  5. 第二十五篇:使用 sigaction 函数实现可靠信号

    前言 在前文中,讲述了一个可靠信号的示例.它分成几个步骤组成( 请参考前文 ).在 Linux 系统编程中,有个方法可以将这些步骤给集成起来,让我们使用起来更加的方便. 那就是调用 sigaction ...

  6. cocos2dx游戏--三国关羽传【角色扮演类】Demo的制作及实现

    项目地址:https://github.com/moonlightpoet/GuanYuZhuan 主要类及其对应效果: MainScene:菜单界面(用于选择不同剧本) StoryScene:故事界 ...

  7. Maven创建一个Web项目

    我们可以通过命令行或者直接使用Eclipse创建一个maven webapp项目:通过命令行创建在命令行中输入如下格式的命令将会创建一个新的maven webapp项目:mvn archetype:g ...

  8. 如何在 Linux 上录制你的终端操作

    导读 录制一个终端操作可能是一个帮助他人学习 Linux .展示一系列正确命令行操作的和分享知识的通俗易懂方法.不管是出于什么目的,从终端复制粘贴文本需要重复很多次,而录制视频的过程也是相当麻烦,有时 ...

  9. Vmware虚拟机中安装centos,并实现联网

    1 安装所需要的软件 vmware workstation 12 永久激活码:5A02H-AU243-TZJ49-GTC7K-3C61N CentOS-7-x86_64-Minimal-1708 2 ...

  10. 170309、MySQL存储引擎MyISAM与InnoDB区别总结整理

    1.MySQL默认存储引擎的变迁 在MySQL 5.1之前的版本中,默认的搜索引擎是MyISAM,从MySQL 5.5之后的版本中,默认的搜索引擎变更为InnoDB. 2.MyISAM与InnoDB存 ...