Educational Codeforces Round 40 (Rated for Div. 2)
自信不被FST
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
The first line of the input contains one integer n (1 ≤ n ≤ 100) — the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Print the minimum possible length of the sequence of moves after all replacements are done.
5
RUURU
3
17
UUURRRRRUUURURUUU
13
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
UR和RU都可以代替为D,URU的话当然怎么选都行,顺序不会影响个数,虽然影响了这个序列
#include<bits/stdc++.h> using namespace std; int main() { int n; cin>>n; string s; cin>>s; int ans=0; for(int i=0;s[i];i++) { ans++; if(s[i]=='U') { if(s[i+1]=='R') i++; } else if(s[i]=='R') { if(s[i+1]=='U') i++; } } cout<<ans; return 0; }
本文来自博客园,作者:暴力都不会的蒟蒻,转载请注明原文链接:https://www.cnblogs.com/BobHuang/p/8625855.html
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步