A. Diagonal Walking
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Input

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.

Output

Print the minimum possible length of the sequence of moves after all replacements are done.

Examples
input

Copy
5
RUURU
output

Copy
3
input

Copy
17
UUURRRRRUUURURUUU
output

Copy
13
Note

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替换,求最多可以替换多少个。STL的使用。

【代码】:

#include<bits/stdc++.h>

using namespace std;
#define ll long long
#define maxn 100010
ll a[maxn];
int n; int main()
{
while(cin >> n){
int cnt = n, pos;
string s;
cin >> s;
for(int i=; i<s.size(); i++){
if((s[i]=='R'&&s[i+]=='U') || (s[i]=='U'&&s[i+]=='R')){
s.replace(i,,"D");
}
}
cout << s.size() << endl;
}
return ;
}

string-replace


B. String Typing
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a string s consisting of n lowercase Latin letters. You have to type this string using your keyboard.

Initially, you have an empty string. Until you type the whole string, you may perform the following operation:

  • add a character to the end of the string.

Besides, at most once you may perform one additional operation: copy the string and append it to itself.

For example, if you have to type string abcabca, you can type it in 7 operations if you type all the characters one by one. However, you can type it in 5 operations if you type the string abc first and then copy it and type the last character.

If you have to type string aaaaaaaaa, the best option is to type 4 characters one by one, then copy the string, and then type the remaining character.

Print the minimum number of operations you need to type the given string.

Input

The first line of the input containing only one integer number n (1 ≤ n ≤ 100) — the length of the string you have to type. The second line containing the string s consisting of n lowercase Latin letters.

Output

Print one integer number — the minimum number of operations you need to type the given string.

Examples
input

Copy
7
abcabca
output

Copy
5
input

Copy
8
abcdefgh
output

Copy
8
Note

The first test described in the problem statement.

In the second test you can only type all the characters one by one.

【题意】:

用最少的步数得到给定字符串。你可以进行以下两个操作:1、将一个字符放在字符串的最后;2、(只能使用一次)将该字符串复制并粘贴在最后面。以上操作每进行一次算一步。

【分析】:

基本思路:找两个相等的字符串,满足两个字符串相邻 && 第一个字符串的开头是给定字符串的开头,输出(总长度-相等字符串长度+1)。如果没有找到,直接输出字符串长。

【代码】:

#include<bits/stdc++.h>

using namespace std;
#define ll long long
#define maxn 100010
ll a[maxn];
int n;
string s;
int main()
{
while(cin >> n >> s){
int ans = n;
for(int i=; i<=n/; i++){
if(s.substr(,i) == s.substr(i,i)){
ans = n - i + ;
}
}
cout << ans << endl;
}
return ;
}

string-substr

Educational Codeforces Round 40 (Rated for Div. 2)的更多相关文章

  1. Educational Codeforces Round 40 (Rated for Div. 2) Solution

    从这里开始 小结 题目列表 Problem A Diagonal Walking Problem B String Typing Problem C Matrix Walk Problem D Fig ...

  2. Educational Codeforces Round 40 (Rated for Div. 2) 954G G. Castle Defense

    题 OvO http://codeforces.com/contest/954/problem/G 解 二分答案, 对于每个二分的答案值 ANS,判断这个答案是否可行. 记 s 数组为题目中描述的 a ...

  3. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  4. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

  5. Educational Codeforces Round 43 (Rated for Div. 2)

    Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...

  6. Educational Codeforces Round 35 (Rated for Div. 2)

    Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...

  7. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...

  8. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...

  9. Educational Codeforces Round 63 (Rated for Div. 2) 题解

    Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...

随机推荐

  1. HDU1301 Jungle Roads

    Jungle Roads The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign ai ...

  2. spark测试几个hadoop的典型例子

    1.求每年的最高温度数据格式如下: 0067011990999991950051507004888888889999999N9+00001+999999999999999999999900670119 ...

  3. Android Kotlin适用小函数

    都是一些Android适用的Kotlin小函数. 1.点击空白隐藏键盘 //点击空白隐藏键盘 override fun onTouchEvent(event: MotionEvent): Boolea ...

  4. Windows下如何用CMD命令跳转到指定的目录下

    以Window7为例说明,想要跳转到I:\adt-bundle-windows-x86-20130219\sdk\platform-tools目录下. 1.在运行中打开CMD命令窗口如下图所示: 2. ...

  5. Active Directory 站点和服务

    Active Directory 站点和服务 更新时间: 2012年3月 应用到: Windows Server 2008, Windows Server 2008 R2, Windows Serve ...

  6. app分享代码

    友推是一款是面向移动应用的SDK分享组件,提供给开发者集成使用.通过友推,开发者可以轻松集成社会化分享功能,同时创建及管理推荐好友使用您应用的推荐奖励活动,用户推荐好友安装使用您的应用即可获得推荐奖励 ...

  7. python 模块初识

    python的强大之处在于有丰富的实现各种功能的标准库和第三方库,另外还允许用户自己建立库文件, 标准模块(又称为库)包括sys, os, glob, socket, threading, _thre ...

  8. 自己搭建一个记笔记的环境记录(leanote)

    一直在找一个开源的记笔记的软件,偶然看到leanote.竟然还是开源的,还是国人开发的果断mark了.自己在电脑上搭建了一个挺好玩的.可以记录一些不给别人看的小秘密. 下面是步骤记录,当然可以到官网上 ...

  9. [oldboy-django][5python基础][高级特性]Iterator迭代器

    # 区分可迭代对象iterable, 迭代器iterator, 生成器generator a. iterable 可直接用for循环的对象,都称为可迭代对象, from collections imp ...

  10. Struts2请求流程

    1. 一个请求在Struts2框架中的处理步骤: a) 客户端初始化一个指向Servlet容器的请求: b) 根据Web.xml配置,请求首先经过ActionContextCleanUp过滤器,其为可 ...