【枚举】【最小表示法】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 ...
随机推荐
- js_判断当前页面是否有网络和网络连接超时
2018-04-12 方法一:通过navigator.onLine属性判断,返回true为有联网状态,false为断网状态. //方法一 if(navigator.onLine) { console. ...
- Spring cloud 实战读书笔记
基础知识 Spring cloud 版本说明 Brixton.SR5 :Brixton 的第5个Release版本 SRX:service releases 简称SRX版本,X版本号 Spring b ...
- Vue-$emit的用法
1.父组件可以使用 props 把数据传给子组件.2.子组件可以使用 $emit 触发父组件的自定义事件. vm.$emit( event, arg ) //触发当前实例上的事件 vm.$on( ev ...
- Linux静态库和共享库【转】
转自:http://www.cnblogs.com/zlcxbb/p/6806269.html 1.什么是静态库 静态库类似windows中的静态lib 关于windows中的静态lib,可参考 Wi ...
- Linux NAPI/非NAPI
本文主要介绍二层收包流程,包括NAPI与非NAPI方式: NAPI:数据包到来,第一个数据包产生硬件中断,中断处理程序将设备的napi_struct结构挂在当前cpu的待收包设备链表softnet_d ...
- 安全测试===Web 安全渗透方面的学习路线
作者:向生李链接:https://www.zhihu.com/question/21914899/answer/39344435来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...
- Linux时间子系统之八:动态时钟框架(CONFIG_NO_HZ、tickless)【转】
转自:http://blog.csdn.net/droidphone/article/details/8112948 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 数据结 ...
- libsensor
https://github.com/easyiot-china/libsensor https://github.com/adafruit/DHT-sensor-library https://gi ...
- linux 命令行远程登录 后台运行命令的方法
linux 命令行远程登录 后台运行命令的方法 http://blog.csdn.net/isuker/article/details/55061595 Linux 技巧:让进程在后台可靠运行的几种方 ...
- C#反射动态调用dll中的方法及使用QuartZ.net实现作业调度
using Quartz; using Quartz.Impl; using System; using System.Collections.Generic; using System.Linq; ...