题面

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. 齐博x1头部底部菜单高亮设置

    下面这段是默认模板头部的导航菜单: {php}$menu_choose=config('system_dirname')?config('system_dirname'):'index';{/php} ...

  2. 云小课|MRS基础原理之MapReduce介绍

    阅识风云是华为云信息大咖,擅长将复杂信息多元化呈现,其出品的一张图(云图说).深入浅出的博文(云小课)或短视频(云视厅)总有一款能让您快速上手华为云.更多精彩内容请单击此处. 摘要:MapReduce ...

  3. 三、Ocelot请求聚合与负载均衡

    上一篇文章介绍了在.Net Core中如何使用Ocelot:https://www.cnblogs.com/yangleiyu/p/16847439.html 本文介绍在ocelot的请求聚合与负载均 ...

  4. JS逆向实战6-- x轴 y轴 过点触验证码

    点触验证码是一种常见的反爬手段 解决方案有两种:一种是直接解决,这需要深度学习机器学习等图像处理技术,以大量的数据训练识别模型,最终达到模型足矣识别图片中的文字提示和应该点击的区域之间的对应关系. 这 ...

  5. Ajax(下)

    跨域 跨域的概念:非同源请求,均为跨域.如果两个页面拥有相同的协议(protocol),端口(port)和主机(host),那么这两个页面就属于同一个源(origin). 例如:主机:http://w ...

  6. Spring Boot框架下实现Excel服务端导入导出

    Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.今天 ...

  7. 基于python的数学建模---传染病六个模型

    六个模型的区别 SI-Model import scipy.integrate as spi import numpy as np import matplotlib.pyplot as plt # ...

  8. 【Serverless】Unity快速集成认证服务实现邮件登录

    ​概述: 认证服务可以为您的应用快速构建安全可靠的用户认证系统,您只需在应用中访问认证服务的相关能力,而不需要关心云侧的设施和实现. 本次将带来如何使用Unity编辑器快速集成认证服务SDK并实现邮箱 ...

  9. C ++:树

    C++:树 树的概念: 所谓"树"是输就结构的一种,树大概可以分为两大类: 有根树 和 无根树 有根树使有一个确定的根节点,反之为无根树 · 子节点:从树根开始,通过树边向下扩展的 ...

  10. 关于python函数传参

    必须参数 最常见的传参 def say(arg): print(arg) say("Hello world") 输出: Hello world 默认参数 def say(arg, ...