AtCoder Regular Contest 148 B - dp
题面
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的更多相关文章
- AtCoder Regular Contest 093
AtCoder Regular Contest 093 C - Traveling Plan 题意: 给定n个点,求出删去i号点时,按顺序从起点到一号点走到n号点最后回到起点所走的路程是多少. \(n ...
- AtCoder Regular Contest 094
AtCoder Regular Contest 094 C - Same Integers 题意: 给定\(a,b,c\)三个数,可以进行两个操作:1.把一个数+2:2.把任意两个数+1.求最少需要几 ...
- AtCoder Regular Contest 096
AtCoder Regular Contest 096 C - Many Medians 题意: 有A,B两种匹萨和三种购买方案,买一个A,买一个B,买半个A和半个B,花费分别为a,b,c. 求买X个 ...
- AtCoder Regular Contest 097
AtCoder Regular Contest 097 C - K-th Substring 题意: 求一个长度小于等于5000的字符串的第K小子串,相同子串算一个. K<=5. 分析: 一眼看 ...
- AtCoder Regular Contest 098
AtCoder Regular Contest 098 C - Attention 题意 给定一个只包含"E","W"字符串,可以花一的花费使他们互相转换.选定 ...
- Atcoder regular Contest 073(D - Simple Knapsack)
Atcoder regular Contest 073(D - Simple Knapsack) 传送门 因为 w1≤wi≤w1+3 这个特殊条件,我们可以将每个重量离散化一下,同时多开一维记录选择的 ...
- AtCoder Regular Contest 061
AtCoder Regular Contest 061 C.Many Formulas 题意 给长度不超过\(10\)且由\(0\)到\(9\)数字组成的串S. 可以在两数字间放\(+\)号. 求所有 ...
- AtCoder Regular Contest 094 (ARC094) CDE题解
原文链接http://www.cnblogs.com/zhouzhendong/p/8735114.html $AtCoder\ Regular\ Contest\ 094(ARC094)\ CDE$ ...
- AtCoder Regular Contest 092
AtCoder Regular Contest 092 C - 2D Plane 2N Points 题意: 二维平面上给了\(2N\)个点,其中\(N\)个是\(A\)类点,\(N\)个是\(B\) ...
- AtCoder Regular Contest 095
AtCoder Regular Contest 095 C - Many Medians 题意: 给出n个数,求出去掉第i个数之后所有数的中位数,保证n是偶数. \(n\le 200000\) 分析: ...
随机推荐
- 4.websocket基本概念
websockey的模式就是在于当前端向后端发送请求创建一个websocket链连接之后,连接默认不断开,前端和服务端就维护了一个连接,前端可以通过连接给服务端发消息,服务端也可以通过连接给前端发消息 ...
- Nginx配置-1
1.绑定nginx到指定cpu [root@nginx conf.d]# vim /apps/nginx/conf/nginx.conf worker_processes 2; worker_cpu_ ...
- redux中间件
Redux 中间件 什么是中间件? 中间件本质上就是一个函数,Redux允许我们通过中间件的方式,扩展和增强Redux应用程序,增强体现在对action处理能力上,之前的计数器与弹出框案例中.acti ...
- devexpress中searchLookUpEdit赋值不显示
给searchLookUpEdit进行赋值的时候使用 string str="123"; searchLookUpEdit1.EditValue = str; 一直不显示或者显示为 ...
- mongorestore target dump invalid CreateFile dump The system cannot find
问题 使用 mongorestore 指定 dump 文件夹,恢复数据报错. mongorestore target 'dump' invalid: CreateFile dump: The syst ...
- 关于linux更改root用户下面的鼠标样式
前言 这几天一直研究关于 lightmd 显示管理器(也就是刚进入系统的用户登录界面)的主题配置问题,我发现鼠标样式和登录个人用户的鼠标样式不一样(之前我也发现了,懒得捣鼓)今天抽出时间研究了一下,这 ...
- MySQL锁,锁的到底是什么?
MySQL锁系列文章已经鸽了挺久了,最近赶紧挤了挤时间,和大家聊一聊MySQL的锁. 只要学计算机,「锁」永远是一个绕不过的话题.MySQL锁也是一样. 一句话解释MySQL锁: MySQL锁是解决资 ...
- 【算法总结】【队列均LinkedList】栈和队列、双端队列的使用及案例
1.栈 初始化:Stack<E> stack = new Stack<>(); 出栈:stack.pop() 或 stack.remove(stack.size() - 1) ...
- async.js 版本兼容问题 async.filter举例
async3.x 和async2.6.1 版本下 const files = ['dir1/file1.txt','dir2/file3.txt','dir3/file6.txt']; // Usin ...
- Python Excel 处理模块 : OpenPyXL
OpenPyXL模块使用方法 以下是介绍OpenPyXL的基本使用,不涉及样式和合并单元格的跨行操作 一般来说,对于大名鼎鼎的xlrd,xlwt和xlutils三个模块,Excel操作有3个基本状态 ...