zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 40 (Rated for Div. 2)

    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=0; i<s.size(); i++){
                if((s[i]=='R'&&s[i+1]=='U') || (s[i]=='U'&&s[i+1]=='R')){
                    s.replace(i,2,"D");
                }
            }
            cout << s.size() << endl;
        }
        return 0;
    }
    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=1; i<=n/2; i++){
                if(s.substr(0,i) == s.substr(i,i)){
                    ans = n - i + 1;
                }
            }
            cout << ans << endl;
        }
        return 0;
    }
    string-substr
  • 相关阅读:
    页面监控容器实现记录
    负载均衡基础理论
    asp.net部署时加密config文件
    还原bak到localdb的问题:The logical database file cannot be found ldf
    Could not load file or assembly 'System.Data.SQLite ... 试图加载格式不正确的程序
    Window vista 以上制作自定义证书并为端口配置ssl
    1-6、算法设计常用思想之迭代法
    1-5、算法设计常用思想之穷举法
    1-4、算法设计常用思想之动态规划法
    游戏开发-cocos creator踩坑-bind(this)导致的事件监听off不掉
  • 原文地址:https://www.cnblogs.com/Roni-i/p/8877220.html
Copyright © 2011-2022 走看看