【枚举】【最小表示法】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem F. Matrix Game
给你一个n*m的字符矩阵,将横向(或纵向)全部裂开,然后以任意顺序首尾相接,然后再从中间任意位置切开,问你能构成的字典序最大的字符串。
以横向切开为例,纵向类似。
将所有横排从大到小排序,枚举最后切开的位置在哪一横排,将这一排提到排序后的字符串数组最前面,求个“最大表示法”,如果最大表示法的位置恰好在第一排的位置,那么可以用来更新答案。
如果不在第一排的位置,那么其所构成的仍然是合法的串,而且一定不会影响答案。
这是一个最小表示法的板子。
#include<cstdio>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
string a[105],ans,b[105];
int n,m;
bool cmp(const string &a,const string &b){
return a>b;
}
int MaxRep(string s, int l)
{
int i,j,k;
i=0;j=1;k=0;
while(i<l&&j<l)
{
k=0;
while(s[i+k]==s[j+k]&&k<l) k++;
if(k==l) return i;
if(s[i+k]<s[j+k]) //¸Ä³É´óÓÚ¾ÍÊÇ×îС±íʾ
if(i+k+1>j) i=i+k+1;
else i=j+1;
else if(j+k+1>i) j=j+k+1;
else j=i+1;
}
if(i<l) return i;
else return j;
}
int main(){
// freopen("f.in","r",stdin);
scanf("%d%d",&n,&m);
for(int i=0;i<n;++i){
cin>>a[i];
}
for(int i=0;i<n;++i){
for(int j=0;j<m;++j){
b[j]+=a[i][j];
}
}
sort(a,a+n,cmp);
for(int i=0;i<n;++i){
string t=a[i];
for(int j=0;j<n;++j){
if(j!=i){
t+=a[j];
}
}
// cout<<i<<": "<<t<<' ';
int p=MaxRep(t,n*m);
string pre=t.substr(0,p);
t.erase(0,p);
t+=pre;
// cout<<t<<endl;
ans=max(ans,t);
}
sort(b,b+m,cmp);
for(int i=0;i<m;++i){
string t=b[i];
for(int j=0;j<m;++j){
if(j!=i){
t+=b[j];
}
}
// cout<<i<<": "<<t<<' ';
int p=MaxRep(t,n*m);
string pre=t.substr(0,p);
t.erase(0,p);
t+=pre;
// cout<<t<<endl;
ans=max(ans,t);
}
for(int i=0;i<n*m;++i){
if(i!='0'){
for(int j=i;j<n*m;++j){
putchar(ans[j]);
}
puts("");
return 0;
}
}
return 0;
}
【枚举】【最小表示法】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 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 I. Minimum Prefix
给你n个字符串,问你最小的长度的前缀,使得每个字符串任意循环滑动之后,这些前缀都两两不同. 二分答案mid之后,将每个字符串长度为mid的循环子串都哈希出来,相当于对每个字符串,找一个与其他字符串所选 ...
- 【找规律】【DFS】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem A. Arithmetic Derivative
假设一个数有n个质因子a1,a2,..,an,那么n'=Σ(a1*a2*...*an)/ai. 打个表出来,发现一个数x,如果x'=Kx,那么x一定由K个“基础因子”组成. 这些基础因子是2^2,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: ...
- 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 E. Space Tourists
给你n,K,问你要选出最少几个长度为2的K进制数,才能让所有的n位K进制数删除n-2个元素后,所剩余的长度为2的子序列至少有一个是你所选定的. 如果n>K,那么根据抽屉原理,对于所有n位K进制数 ...
- 【推导】【贪心】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem D. Clones and Treasures
给你一行房间,有的是隐身药水,有的是守卫,有的是金币. 你可以任选起点,向右走,每经过一个药水或金币就拿走,每经过一个守卫必须消耗1个药水,问你最多得几个金币. 药水看成左括号,守卫看成右括号, 就从 ...
- 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 GameInput file: standard inputOutput file: standard inputTime limit: 1 secondM ...
随机推荐
- ES6新用法
ES6 详细参考页面 简介 ECMAScript和JavaScript的关系是,前者是后者的规格,后者是前者的一种实现.一般来说,这两个词是可以互换的. let命令 ES6新增了let命令,用来声明变 ...
- hdu 1081 To The Max(dp+化二维为一维)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1081 To The Max Time Limit: 2000/1000 MS (Java/Others ...
- 20179205《Linux内核原理与分析》第一周作业
输出 shiyanlou 图形字符的命令banner: 新建用户wangyazhe,输入密码不会显示出来: 利用sudo adduser添加一个用户 loutest,mkdir创建一个新的文件夹opt ...
- Centos7的iso everything与DVD以及Live的区别
DVD.ISO 可以用安装程序安装的所有安装包,推荐镜像 Netinstall.iso 从网络安装或者救援系统 Everything.iso 包含centos7的一套完整的软件包,可以用来安装系统或者 ...
- OOM有哪些情况,SOF有哪些情况
OOM 1.全称为OutOfMemoryError异常,如果虚拟机在扩展栈时无法申请足够的内存空间,抛出它: 2.Java heap异常:java.lang.OutOfMemoryError:Java ...
- Linking code for an enhanced application binary interface (ABI) with decode time instruction optimization
A code sequence made up multiple instructions and specifying an offset from a base address is identi ...
- 【字符串处理算法】字符串包含的算法设计及C代码实现【转】
转自:http://blog.csdn.net/zhouzhaoxiong1227/article/details/50679587 版权声明:本文为博主原创文章,对文章内容有任何意见或建议,欢迎与作 ...
- 比 file_get_contents() 更优的 cURL 详解(附实例)
PHP 可以使用 file_get_content() 函数抓取网页内容,但却无法进行更复杂的处理,譬如文件的上传或下载. Cookie 操作等等.而 cURL 提供了这些功能. 一.cURL简介 在 ...
- 工具===代替cmd的conemu设置
conemu设置 Win+Alt+P进入设置界面,字体设置: 隐藏右上角菜单和窗口标题. (Ctrl + ~ 隐藏/显示terminal) 设置背景图片 避免误操作,关闭/新建确认 设置win+w默认 ...
- 【UOJ224】短路
具体可以看UOJmyy的blog,orz 就是一个贪心. #include<bits/stdc++.h> typedef long long ll; using namespace std ...