zoukankan      html  css  js  c++  java
  • CF 1005B Delete from the Left 【模拟数组操作/正难则反】

    You are given two strings s and t. In a single move, you can choose any of two strings and delete the first (that is, the leftmost) character. After a move, the length of the string decreases by 1. You can't choose a string if it is empty.

    For example:

    by applying a move to the string "where", the result is the string "here",
    by applying a move to the string "a", the result is an empty string "".
    You are required to make two given strings equal using the fewest number of moves. It is possible that, in the end, both strings will be equal to the empty string, and so, are equal to each other. In this case, the answer is obviously the sum of the lengths of the initial strings.

    Write a program that finds the minimum number of moves to make two given strings s and t equal.

    Input
    The first line of the input contains s. In the second line of the input contains t. Both strings consist only of lowercase Latin letters. The number of letters in each string is between 1 and 2⋅105, inclusive.

    Output
    Output the fewest number of moves required. It is possible that, in the end, both strings will be equal to the empty string, and so, are equal to each other. In this case, the answer is obviously the sum of the lengths of the given strings.

    Examples
    Input
    test
    west
    Output
    2
    Input
    codeforces
    yes
    Output
    9
    Input
    test
    yes
    Output
    7
    Input
    b
    ab
    Output
    1
    Note
    In the first example, you should apply the move once to the first string and apply the move once to the second string. As a result, both strings will be equal to "est".

    In the second example, the move should be applied to the string "codeforces" 8 times. As a result, the string becomes "codeforces" → "es". The move should be applied to the string "yes" once. The result is the same string "yes" → "es".

    In the third example, you can make the strings equal only by completely deleting them. That is, in the end, both strings will be empty.

    In the fourth example, the first character of the second string should be deleted.

    #include<cstdio>
    #include<string>
    #include<cstdlib>
    #include<cmath>
    #include<iostream>
    #include<cstring>
    #include<set>
    #include<queue>
    #include<algorithm>
    #include<vector>
    #include<map>
    #include<cctype>
    #include<stack>
    #include<sstream>
    #include<list>
    #include<assert.h>
    #include<bitset>
    #include<numeric>
    #define debug() puts("++++")
    #define gcd(a,b) __gcd(a,b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define fi first
    #define se second
    #define pb push_back
    #define sqr(x) ((x)*(x))
    #define ms(a,b) memset(a,b,sizeof(a))
    #define sz size()
    #define be begin()
    #define pu push_up
    #define pd push_down
    #define cl clear()
    #define lowbit(x) -x&x
    #define all 1,n,1
    #define rep(i,x,n) for(int i=(x); i<(n); i++)
    #define in freopen("in.in","r",stdin)
    #define out freopen("out.out","w",stdout)
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ULL;
    typedef pair<int,int> P;
    const int INF = 0x3f3f3f3f;
    const ll LNF = 1e18;
    const int N = 1e3 + 20;
    const int maxm = 1e6 + 10;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int dx[] = {-1,1,0,0,1,1,-1,-1};
    const int dy[] = {0,0,1,-1,1,-1,1,-1};
    int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    string a,b;
    /*
    给你两个字符串,只能选其中一个的最左字符删除,若不能删到相等就输出两串长之和,否则输出删除字符数
    */
    int main()
    {
        while(cin>>a>>b)
        {
            int c=0;
            reverse(a.begin(),a.end());
            reverse(b.begin(),b.end());
            rep(i,0,min(a.size(),b.size()))
            {
                if(a[i]==b[i]) c++;
                else break;
            }
            cout<<a.size()+b.size()-2*c<<endl;
        }
    }
    
  • 相关阅读:
    代码的测试 生产 开关 一键切换 开关
    iot表输出按主键列排序,heap表不是
    iot表输出按主键列排序,heap表不是
    iot 表主键存放所有数据,且按数据插入顺序排序
    iot 表主键存放所有数据,且按数据插入顺序排序
    iot表和heap表排序规则不同
    iot表和heap表排序规则不同
    Oracle 排序规则
    Oracle 排序规则
    perl 异步超时 打印错误
  • 原文地址:https://www.cnblogs.com/Roni-i/p/9347994.html
Copyright © 2011-2022 走看看