暑假练习赛1——Problem D(Delete from the Left,字符串)

原题链接:http://codeforces.com/contest/1005/problem/B

Problem D

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 524288/262144K (Java/Other)

Total Submission(s) : 5   Accepted Submission(s) : 0

Problem Description

Codeforces (c) Copyright 2010-2018 Mike Mirzayanov

Input

<p>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\cdot10^5$$$, inclusive.</p>

Output

<p>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.</p>

Sample Input

 

test west codeforces yes

Sample Output

 

2

9

=================================

题意:给出两个字符串,分别从左往右删除字母,问最少删除几个字母后两个字符串完全相同(包括空串)。。。知道从左往右删后就挺水了。

思路:从右往左对比即可,因为两个字符串如果有相同部分,那么最后的一个或几个字母一定相同。。。

AC代码:

#include<iostream>
#include<cmath>
#include<string>
using namespace std;
int main()
{
    string s1,s2;
    while(cin>>s1>>s2)
    {
        int l1=s1.length(),l2=s2.length();
        int ans=0,num=0;
        for(int i=l1-1,j=l2-1;i>=0&&j>=0;--i,--j)
        {
            if(s1[i]==s2[j])
            {
                num++;
            }
            else
                break;
        }
        ans=l1+l2-num*2;
        cout<<ans<<endl;
    }
    return 0;
}

The end;

猜你喜欢

转载自blog.csdn.net/qq_41661919/article/details/81319152