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. Installing Apps Kattis - installingapps (贪心 + 背包)

    Installing Apps Kattis - installingapps Sandra recently bought her first smart phone. One of her fri ...

  2. 并查集:HDU1213-How Many Tables(并查集最简单的应用)

    How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...

  3. Ubuntu下Python无法识别中文

    在NLP的相关任务中,应用python处理中文是很常见的.在这个过程中,由于编码方式的不一致,可能会出现以下两种错误: 1)SyntaxError:  Non-ASCII character in f ...

  4. 《鸟哥的Linux私房菜》学习笔记(0)——磁盘与文件系统管理

    一.Linux的登陆流程 login: 用户名:每个用户名都有一个用户ID(用户标识符),计算机处理的就是用户ID(数字)而不是用户名(字符),. 认证机制:Authentication,比如密码或者 ...

  5. POJ 2771 Guardian of Decency (二分图最大点独立集)

    Guardian of Decency Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6133   Accepted: 25 ...

  6. models管理类抽取基类

    Models类 models.py # coding:utf-8 from django.db import models from db.Base_model import Base_Model f ...

  7. IOS开发学习笔记013-内存管理

    内存管理 1.基本知识 2.关闭ARC机制 3.@property 4.循环引用 5.自动释放池 6.内存管理总结 一.基本知识 内存的分类 栈:局部变量 堆:动态申请的对象,变量等 全局(静态):s ...

  8. Format aborted in 格式化namenode 失败的原因

    [user6@das0 hadoop-0.20.203.0]$ bin/hadoop namenode -format 12/02/20 14:05:17 INFO namenode.NameNode ...

  9. easyui 右键绑定事件

    $(function(){ $('#hospitalTree').bind('contextmenu', function(e) {            e.preventDefault();   ...

  10. 【bzoj1822】[JSOI2010]Frozen Nova 冷冻波 计算几何+二分+网络流最大流

    题目描述 WJJ喜欢“魔兽争霸”这个游戏.在游戏中,巫妖是一种强大的英雄,它的技能Frozen Nova每次可以杀死一个小精灵.我们认为,巫妖和小精灵都可以看成是平面上的点. 当巫妖和小精灵之间的直线 ...