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

本文解析了Codeforces竞赛中一道关于字符串操作的问题,通过从右往左比较两个字符串的字符,找出最少删除次数使两字符串相等的算法。提供了一段简洁的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原题链接: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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值