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. A * B Problem Plus HDU - 1402 (FFT)

    A * B Problem Plus HDU - 1402 (FFT) Calculate A * B.  InputEach line will contain two integers A and ...

  2. [Hdu3652]B-number(数位DP)

    Description 题目大意:求小于n是13的倍数且含有'13'的数的个数. (1 <= n <= 1000000000) Solution 数位DP,题目需要包含13,且被13整除, ...

  3. 第5模块闯关CSS练习题

    1.列举你知道的css选择器? 说道css选择器,大家都知道有许多种,但是真要你去掰着手指头数一数的话,你可能需要数几分钟.其实这么多选择器,完全可以分为两类: 标签选择器(*是特殊情况),可但标签, ...

  4. Android stadio 电脑连上手机可以识别,但是连不上Android stadio

    原来是因为电脑没有装Android 手机驱动,我电脑刚装了系统. 很多驱动没有装.我有一个联想驱动管理,提示我装Android手机驱动.装完之后,就可以识别到手机了. 如果你的手机在电脑不识别,那么装 ...

  5. Redis的概述、优势和安装部署

    Redis概述 Redis是一个开源,先进的key-value存储,并用于构建高性能,可扩展的应用程序的完美解决方案. Redis从它的许多竞争继承来的三个主要特点: Redis数据库完全在内存中,使 ...

  6. 使用code::blocks编译windows的dll链接库

    因为机子上没有安装Visual Studio,所以找到了一种通过code::blocks编译dll的方式,踩到的坑是code::blocks默认的compiler是32位的,这样编译出的dll也是32 ...

  7. ogre3D,cegui配置问题

    今天按照网上的教程配置CEGUI, 一直运行不了,不明白原因,而后又出现了错误 LNK1104: 无法打开文件“OgreGUIRenderer_d.lib”,经过反复检查,排除包含目录问题. 不过可能 ...

  8. Python之利用socketserver实现并发

    socketserver这个模块是利用IO多路复用以及多线程实现并发的,可以让服务器同时建立多个链接 原理如图 我们这样更改服务器代码 import socketserver '''需要先写上一个类继 ...

  9. c++11特性使用

    #include <list> #include <iostream> int main(){ list<int> lst; for(list<int> ...

  10. github 下载部分文件夹

    1.下载svn: 记得勾上: 2.配置环境变量,将svn的bin目录添加到环境变量 3.svn checkout [链接](你的下载的项目地址) 不过,注意,要改一下哦: 比如我要下载todo项目里的 ...