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 |
思路:
贪心的将分隔串按字典序从大到小排序。然后枚举把一个串放到第一个串前面,判断是否产生更优解。
判断最优解的过程用字符串的最大最小表示法即可。
#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的更多相关文章
- 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 ...
- 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 ...
- 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: ...
- 【二分】【字符串哈希】【二分图最大匹配】【最大流】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的循环子串都哈希出来,相当于对每个字符串,找一个与其他字符串所选 ...
- 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: ...
- 【二分图】【并查集】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),有一些炸弹,你可以选择一个空的位置,再放一个炸弹并将其引爆,一个炸弹爆炸后,其所在行和列的所有炸弹都会爆炸,连锁反应. 问你所能引爆的最多炸 ...
- 【动态规划】【滚动数组】【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 ...
- 【枚举】【最小表示法】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem F. Matrix Game
给你一个n*m的字符矩阵,将横向(或纵向)全部裂开,然后以任意顺序首尾相接,然后再从中间任意位置切开,问你能构成的字典序最大的字符串. 以横向切开为例,纵向类似. 将所有横排从大到小排序,枚举最后切开 ...
- 【推导】【构造】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进制数 ...
随机推荐
- LodRunner实现大负载测试的四部曲(配置系统参数、配置LR、修改脚本、设置组策略)
见 http://www.51testing.com/?uid-97659-action-viewspace-itemid-210924 LoadRunner以下简称(LR)是目前业界最流行的压力测试 ...
- U盘插入拔出提示
Unit Unit1; Interface Uses Windows, Messages, SysUtils, Variants, classes, Graphics, Controls, Forms ...
- 【POJ2516】Minimum Cost
[POJ2516]Minimum Cost 题意:有N个收购商.M个供应商.K种物品.对于每种物品,每个供应商的供应量和每个收购商的需求量已知.每个供应商与每个收购商之间运送该物品的运费已知.求满足收 ...
- linux的简单查找的方法
catalina.out文件查找指定行sed -n 346492p catalina.out 查找第几到第几行sed -n 346200,346692p catalina.out 查找指定内容(不区分 ...
- SQL使用union合并查询结果(转载)
1.UNION的作用 UNION 指令的目的是将两个 SQL 语句的结果合并起来.从这个角度来看, UNION 跟 JOIN 有些许类似,因为这两个指令都可以由多个表格中撷取资料. UNION 的一 ...
- ionic 上拉加载问题(分页)
问题描述: 1.第一初始化时执行了上拉加载更多. 2.上拉时存在执行多次加载动作. angularjs的ajax不提供同步机制,是为了防止页面长时间等待,很多时候我们又需要这种同步机制交换状态,比如上 ...
- SQL Server中执行Sql字符串,返回执行结果
今天遇到一个问题:想把sql字符串在SQL Server 中执行了,并获取执行的结果 ); SET @tablename='select @table3 = count(1) from UserVis ...
- 针对Quant的Python快速入门指南
作者:用Python的交易员 (原创文章,转载请注明出处) 最近有越来越多的朋友在知乎或者QQ上问我如何学习入门Python,就目前需求来看,我需要写这么一篇指南. 针对整个vn.py框架的学习,整体 ...
- MapReduce分析流量汇总
一.MapReduce编程规范 一.MapReduce编程规范 用户编写mr程序主要分为三个部分:Mapper,Reducer,Driver 1.Mapper阶段 (1)用户自定义Mapper类 要继 ...
- 5.Git版本库创建
1.什么是版本库呢? 什么是版本库呢?版本库又名仓库,英文名repository,你可以简单理解成一个目录,这个目录里面的所有文件都可以被Git管理起来,每个文件的修改.删除,Git都能跟踪,以便任何 ...