954A Diagonal Walking

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
3
input
Copy
17
UUURRRRRUUURURUUU
output
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).



题意:输入n个长度的字符串,如果有两个相邻的UR或者RU都可以替换为一个D。求替换后的最小长度。


题解:队列模拟     我用的字符数组模拟队列,一个一个添加字符,当有相邻的UR就长度减少一个,然后把留下的那个替换为D,所以是len-=2,然后继续添加,有相邻的就继续前面的操作,直到添加完。不过要注意小标和长度的关系。输出最后的长度就行。当然还有简单的方法,那就是遍历所有字符遇到不想等相邻的字符,就l总长度减少一个,跳过一个字符,原理和队列差不多。给出两种代码

队列模拟AC代码:
#include
using namespace std;
int main()
{
    char s[110],c;
    int len=0,n;
    cin>>n;
    for(int i=0;i>c;
        s[len++]=c;
        if(len>1&&s[len-2]!=s[len-1]&&s[len-2]!='D')
        {
            len-=2;
          s[len++]='D';
        }
    }
    s[len++]='\0';
  cout<

简短代码:
#include
using namespace std;
int n,ans;
string s;
int main()
{
    cin>>n>>s;
    ans=n;
    for(int i=0; i


你可能感兴趣的:(ACM日常水题练习集,队列模拟)