【枚举】【最小表示法】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 ...
随机推荐
- margin 居中
左右auto加个宽度.margin-left: auto; margin-right: auto; width:640px;
- sql server 在作业中 远程连接 oracle mysql sqlserver 数据库
在作业中执行远程连接时,需要对本次作业执行的步骤指定特定用户 并且该用户必须拥有所需操作数据库的db_owner角色,和服务器sysadmin角色 在作业中执行远程连接时,需要做登录映射 下面是我在作 ...
- css3_box-shadow使用记录
1.box-shadow这个属性有6个参数可设置,使用的时候比较少用,每次使用的时候都会忘记,故写此文作记录. 样式: /*1.添加此属性添加阴影*/ box-shadow: 0 0 10px 10p ...
- js_如何优化你的代码让它更好看
1.对于美的东西我们很难拒绝,比如美女.哈哈哈,程序员的梗. 2.所以我希望我写出来的代码也是很美观的,让人看起来会很舒服. 3.要想让你的代码简约美观,就要涉及封装,模块化了,可复用代码.vue可以 ...
- python基础===isinstance() 函数,判断一个对象是否是一个已知的类型
isinstance(object, classinfo) object -- 实例对象. classinfo -- 可以是直接或间接类名.基本类型或者有它们组成的元组. >>>a ...
- 爬虫===登陆CSDN的方法
本文主要介绍csdn的登陆,可应用在爬虫上~ # -*- coding:utf-8 -*- import json import requestsfrom xlutils.copy import co ...
- <摘录>Fedora添加国内源和本地源
<摘录>Fedora添加国内源和本地源 http://www.cnblogs.com/hummersofdie/p/3915070.html fedora的软件源信息文件(*.repo)都 ...
- Centos_Lvm_Create pv vg lv and mount
re-scan new disks without restarting CentOS re-scan new disks(/dev/sdc): #ls /sys/class/scsi_host/ h ...
- JAVA 线程状态及转化
线程状态图 说明:线程共包括以下5种状态.1. 新建状态(New) : 线程对象被创建后,就进入了新建状态.例如,Thread thread = new Thread().2. 就绪状 ...
- Bzoj-2301 [HAOI2011]Problem b 容斥原理,Mobius反演,分块
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2301 题意:多次询问,求有多少对数满足 gcd(x,y)=k, a<=x<=b ...