题面

For a string \(T\) of length \(L\) consisting of d and p, let \(f(T)\) be \(T\) rotated \(180\) degrees. More formally, let \(f(T)\) be the string that satisfies the following conditions.

\(f(T)\) is a string of length \(L\) consisting of d and p.

For every integer \(i\) such that \(1 \leq i \leq L\), the \(i\)-th character of \(f(T)\) differs from the \((L + 1 - i)\)-th character of \(T\).

For instance, if \(T =\) ddddd, \(f(T) =\) ppppp; if \(T =\) dpdppp, \(f(T)=\) dddpdp.

You are given a string \(S\) of length \(N\) consisting of d and p.

You may perform the following operation zero or one time.

Choose a pair of integers \((L, R)\) such that \(1 \leq L \leq R \leq N\), and let \(T\) be the substring formed by the \(L\)-th through \(R\)-th characters of \(S\). Then, replace the \(L\)-th through \(R\)-th characters of \(S\) with \(f(T)\).

For instance, if \(S=\) dpdpp and \((L,R)=(2,4)\), we have \(T=\) pdp and \(f(T)=\) dpd, so \(S\) becomes ddpdp.

Print the lexicographically smallest string that \(S\) can become.

1≤N≤5000

简要题意

给出一个长度为 \(N(1 \le N \le 5000)\) 的字符串 \(S\),且 \(\forall S_i,S_i \in \{d,p\}\)。

定义 \(\operatorname{rotate}(l,r)\),对于区间 \([l,r]\) 的每一个 \(i\),重新赋值 \(S_i\),使得 \(S_i \neq S_{r+l-i}\)。(即,对称的)

你需要执行0或1次 \(\operatorname{rotate}(l,r)\)(\(l,r\) 是任意的),使得 \(S\) 字典序最小。

思路

考虑贪心,遇到的第一个 \(p\) 为 \(l\),然后暴力枚举 \(r\) 找答案即可。

时间复杂度 \(O(N^{2})\)。

代码

#include <bits/stdc++.h>
#define int long long
using namespace std; int n;
string s,bk,ret;
int l;
int r[1000005],tot; signed main(){
cin>>n;
cin>>s;
for(int i=0;i<n;i++){
if(s[i]=='d'){
continue;
}
else{
l=i;
break;
}
}
bk=s;
ret=s;
for(int i=l;i<n;i++){
s=bk;
int L=l,R=i;
for(int j=L,k=R;j<=R;j++,k--){
if(bk[k]=='d'){
s[j]='p';
}
else{
s[j]='d';
}
}
if(s<ret){
ret=s;
}
}
cout<<ret<<'\n';
return 0;
}

AtCoder Regular Contest 148 B - dp的更多相关文章

  1. AtCoder Regular Contest 093

    AtCoder Regular Contest 093 C - Traveling Plan 题意: 给定n个点,求出删去i号点时,按顺序从起点到一号点走到n号点最后回到起点所走的路程是多少. \(n ...

  2. AtCoder Regular Contest 094

    AtCoder Regular Contest 094 C - Same Integers 题意: 给定\(a,b,c\)三个数,可以进行两个操作:1.把一个数+2:2.把任意两个数+1.求最少需要几 ...

  3. AtCoder Regular Contest 096

    AtCoder Regular Contest 096 C - Many Medians 题意: 有A,B两种匹萨和三种购买方案,买一个A,买一个B,买半个A和半个B,花费分别为a,b,c. 求买X个 ...

  4. AtCoder Regular Contest 097

    AtCoder Regular Contest 097 C - K-th Substring 题意: 求一个长度小于等于5000的字符串的第K小子串,相同子串算一个. K<=5. 分析: 一眼看 ...

  5. AtCoder Regular Contest 098

    AtCoder Regular Contest 098 C - Attention 题意 给定一个只包含"E","W"字符串,可以花一的花费使他们互相转换.选定 ...

  6. Atcoder regular Contest 073(D - Simple Knapsack)

    Atcoder regular Contest 073(D - Simple Knapsack) 传送门 因为 w1≤wi≤w1+3 这个特殊条件,我们可以将每个重量离散化一下,同时多开一维记录选择的 ...

  7. AtCoder Regular Contest 061

    AtCoder Regular Contest 061 C.Many Formulas 题意 给长度不超过\(10\)且由\(0\)到\(9\)数字组成的串S. 可以在两数字间放\(+\)号. 求所有 ...

  8. AtCoder Regular Contest 094 (ARC094) CDE题解

    原文链接http://www.cnblogs.com/zhouzhendong/p/8735114.html $AtCoder\ Regular\ Contest\ 094(ARC094)\ CDE$ ...

  9. AtCoder Regular Contest 092

    AtCoder Regular Contest 092 C - 2D Plane 2N Points 题意: 二维平面上给了\(2N\)个点,其中\(N\)个是\(A\)类点,\(N\)个是\(B\) ...

  10. AtCoder Regular Contest 095

    AtCoder Regular Contest 095 C - Many Medians 题意: 给出n个数,求出去掉第i个数之后所有数的中位数,保证n是偶数. \(n\le 200000\) 分析: ...

随机推荐

  1. 基于BP神经网络的手MNIST写数字识别

    import numpy import math import scipy.special#特殊函数模块 import matplotlib.pyplot as plt #创建神经网络类,以便于实例化 ...

  2. 说说 Redis 事务

    更多技术文章,请关注我的个人博客 www.immaxfang.com 和小公众号 Max的学习札记. Redis 事务简介 Redis 只是提供了简单的事务功能.其本质是一组命令的集合,事务支持一次执 ...

  3. disk磁盘分区软件使用教程,磁盘扩容无损备份

    前几天,因为我的笔记本电脑C盘D盘全红了,趁着双11固态降价,赶紧买了一张三星980 500g 给我的拯救者插上了,加上原来的500g,总共1T,已经够用了. 不得不说拯救者系列预留的1个M.2固态插 ...

  4. Python基础之面向对象:3、继承与派生

    面向对象 一.三大特征之继承 python三大特征: 封装.继承.多态 三者中继承最为核心,实际应用对,感受较为直观 封装和多态略微抽象 1.继承的概念 继承的含义: ​ 在现实生活中,继承表示人与人 ...

  5. 词向量word2vec(图学习参考资料)

    介绍词向量word2evc概念,及CBOW和Skip-gram的算法实现. 项目链接: https://aistudio.baidu.com/aistudio/projectdetail/500940 ...

  6. 一次SpringBoot版本升级,引发的血案

    前言 最近项目组升级了SpringBoot版本,由之前的2.0.4升级到最新版本2.7.5,却引出了一个大Bug. 到底是怎么回事呢? 1.案发现场 有一天,项目组的同事反馈给我说,我之前有个接口在新 ...

  7. perl中 use strict会出现“requires explicit package name”错误

    转载 perl中use strict的用法 如果你使用 use strict 的话,它会强迫你用 my 声明变量,否则将会报上述错误.

  8. php + layui 文件上传 以及 拖拽上传

    HTML: <div id="box"> <div id="drop_area">将文件拖拽到此区域</div> <d ...

  9. 【Devexpress】Gridcontorl动态创建列不显示的问题

    通过代码创建列但是不显示,这个原因是因为代码创建的列Visible属性默认是false 所以需要设置为true就会显示了 gridColumn.Visible = true;

  10. Springboot使用基础总结

    搭建项目 (Eclipse   |  |     IDEA====>官方生成DEMO:http://start.spring.io/) 模版引擎  ( thymeleaf freemarker ...