1、给定一个$n*m$的矩阵,里面的数字是1到$n*m$的一个排列。一个$n*m$矩阵$A$对应一个$n*m$ 的01字符串,字符串的位置$i*m+j$为1当且仅当$A_{i,j}=i*m+j+1$。现在可以交换矩阵的两列或者两行。在任意多次操作后使得矩阵对应的字符串最大?

思路:从1到$n*m$,依次枚举。每次将当前数字填回正确位置。比较该次操作前后是否变大。不变大则不做本次操作。

#include <stdio.h>
#include <vector>
#include <string>
using namespace std; class GridSortMax
{
int a[55][55];
int b[55][55];
int N,M; void reset(int t) {
for(int i=0;i<N;++i) for(int j=0;j<M;++j) {
if(t==0) b[i][j]=a[i][j];
else a[i][j]=b[i][j];
}
} pair<int,int> get(int t) {
for(int i=0;i<N;++i) {
for(int j=0;j<M;++j) {
if(a[i][j]==t) return make_pair(i,j);
}
}
} void swapCol(int y1,int y2) {
for(int i=0;i<N;++i) swap(a[i][y1],a[i][y2]);
} void swapRow(int x1,int x2) {
for(int i=0;i<M;++i) swap(a[x1][i],a[x2][i]);
} string getAns() {
string s="";
for(int i=0;i<N;++i) for(int j=0;j<M;++j) {
if(a[i][j]==i*M+j+1) s+="1";
else s+="0";
}
return s;
} public:
string findMax(int n,int m,vector<int> grid)
{
for(int i=0;i<n;++i) {
for(int j=0;j<m;++j) {
a[i][j]=grid[i*m+j];
}
}
N=n;
M=m;
for(int i=1;i<=n*m;++i) {
pair<int,int> p=get(i);
int x=p.first;
int y=p.second;
int xx=(i-1)/m;
int yy=(i-1)%m;
if(x==xx&&y==yy) continue; string s0=getAns();
reset(0); if(x!=xx) swapRow(x,xx);
if(y!=yy) swapCol(y,yy); string s1=getAns();
if(s1<s0) reset(1); } return getAns(); }
};

  

2、(1)“()”是合法的字符串;(2)如果s是合法的,那么"(sss..ss)"是合法的,即1个或多个s外面加一层圆括号。给定字符串$S$以及 $k$,问$S$的所有合法子列的集合中(不重复),第$k$小的是哪个?$|S|<=150$,$k<=10^{9}$

思路:首先得到所有合法的字符串,然后分别枚举是不是$S$的子列。

#include <string.h>
#include <stdio.h>
#include <vector>
#include <string>
#include <set>
using namespace std; set<string> s; void dfs(string x) {
if(x.size()>150) return;
s.insert(x);
string p="";
for(int i=0;p.size()<=150;++i) {
p+=x;
dfs("("+p+")");
}
} class RepeatedStrings
{
public:
string findKth(string S,int k)
{
dfs("()");
for(string x:s) {
int p1=0,p2=0;
while(p1<(int)S.size()&&p2<(int)x.size()) {
if(S[p1]==x[p2]) ++p1,++p2;
else ++p1;
}
if(p2>=(int)x.size()) {
if(--k==0) return x;
}
}
return "";
}
};

  

topcoder srm 702 div1 -3的更多相关文章

  1. Topcoder SRM 643 Div1 250<peter_pan>

    Topcoder SRM 643 Div1 250 Problem 给一个整数N,再给一个vector<long long>v; N可以表示成若干个素数的乘积,N=p0*p1*p2*... ...

  2. Topcoder Srm 726 Div1 Hard

    Topcoder Srm 726 Div1 Hard 解题思路: 问题可以看做一个二分图,左边一个点向右边一段区间连边,匹配了左边一个点就能获得对应的权值,最大化所得到的权值的和. 然后可以证明一个结 ...

  3. topcoder srm 714 div1

    problem1 link 倒着想.每次添加一个右括号再添加一个左括号,直到还原.那么每次的右括号的选择范围为当前左括号后面的右括号减去后面已经使用的右括号. problem2 link 令$h(x) ...

  4. topcoder srm 738 div1 FindThePerfectTriangle(枚举)

    Problem Statement      You are given the ints perimeter and area. Your task is to find a triangle wi ...

  5. Topcoder SRM 602 div1题解

    打卡- Easy(250pts): 题目大意:rating2200及以上和2200以下的颜色是不一样的(我就是属于那个颜色比较菜的),有个人初始rating为X,然后每一场比赛他的rating如果增加 ...

  6. 【topcoder SRM 702 DIV 2 250】TestTaking

    Problem Statement Recently, Alice had to take a test. The test consisted of a sequence of true/false ...

  7. Topcoder SRM 627 div1 HappyLettersDiv1 : 字符串

    Problem Statement      The Happy Letter game is played as follows: At the beginning, several players ...

  8. Topcoder SRM 584 DIV1 600

    思路太繁琐了 ,实在不想解释了 代码: #include<iostream> #include<cstdio> #include<string> #include& ...

  9. TopCoder SRM 605 DIV1

    604的题解还没有写出来呢.先上605的. 代码去practice房间找. 说思路. A: 贪心,对于每个类型的正值求和,如果没有正值就取最大值,按着求出的值排序,枚举选多少个类型. B: 很明显是d ...

随机推荐

  1. sqli-labs(三)

    第五关:这关的重点是有联合查询的注入漏洞,但是页面不会显示查询信息,但是会有报错信息显示在页面上 这关是双查询注入,其实用报错注入和盲注都是可以注入的,但是我觉得这个双查询注入还是很有意思的,所以这关 ...

  2. MyBatis基础入门《十九》动态SQL(set,trim)

    MyBatis基础入门<十九>动态SQL(set,trim) 描述: 1. 问题 : 更新用户表数据时,若某个参数为null时,会导致更新错误 2. 分析: 正确结果: 若某个参数为nul ...

  3. c#之枚举,结构体

    1.枚举 2.结构体 例子1: using System; using System.Collections.Generic; using System.Linq; using System.Text ...

  4. HTML5特效收录-不定时更新

    在工作中,我们可能会用到各种交互效果.而这些效果在平常翻看文章的时候碰到很多,但是一时半会又想不起来在哪,所以养成知识整理的习惯是很有必要的.希望能给大大家启发,并且学习. HTML5 Canvas粒 ...

  5. spark sql01

    package sql; import org.apache.spark.SparkConf; import org.apache.spark.api.java.JavaSparkContext; i ...

  6. hessian 在spring中的使用 (bean 如 Dao无法注入的问题)

    hessian的主要结构分客户端与服务端,中间基于http传输.客户端主要做的事情是把对远程接口调用序列化为流,并传输到服务端:服务端主要做的事情是把传输过来的流反序列化为对服务的请求,调用相应服务后 ...

  7. python爬虫 ----文章爬虫(合理处理字符串中的\n\t\r........)

    import urllib.request import re import time num=input("输入日期(20150101000):") def openpage(u ...

  8. STL容器之map

    [1]map容器 map 是关联容器.容器中的每一个元素都是由一个键值和一个数据值组成的. set 是一个集合它以其元素作为键值(同一个键值只能出现一次),且默认以升序排列. list 是一个顺序容器 ...

  9. 1.python虚拟环境的安装-用以同时使用py2,py3

    第一步:安装环境支持[linux下在前加sudo] http://www.lfd.uci.edu/~gohlke/pythonlibs/#pycurl pip install virtualenv 第 ...

  10. [DeploymentService:290066]Error occurred while downloading files from admin server for deployment request "0". Underlying error is: "null"

    weblogic 莫名无法启动: <Apr , :: PM CST> <Error> <Deployer> <BEA-> <Failed to i ...