B. Delete from the Left

本文介绍了一种优化的字符串匹配算法,通过比较两个字符串的左端字符并逐步删除不匹配的字符,来实现最少操作次数使两字符串相等的目标。算法特别适用于处理大量文本数据的情况。
B. Delete from the Left
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given two strings ss and tt. 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 11. 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 ss and tt equal.

Input

The first line of the input contains ss. In the second line of the input contains tt. Both strings consist only of lowercase Latin letters. The number of letters in each string is between 1 and 21052⋅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
Copy
test
west
output
Copy
2
input
Copy
codeforces
yes
output
Copy
9
input
Copy
test
yes
output
Copy
7
input
Copy
b
ab
output
Copy
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 "codeforces88 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 <iostream>
#include <cstring>
#include <cmath>

using namespace std;

int main()
{
    string a,b;
    cin>>a>>b;
    int lena=a.length();
    int lenb=b.length();
    int ans=lena+lenb;
    if(lena>=lenb){
        for(int i=lena-1;i>=0;--i)
        {
            if(a[i]==b[--lenb]){ans-=2;}
            else break;
        }
    }
    else{
        for(int i=lenb-1;i>=0;--i)
        {
            if(b[i]==a[--lena]){ans-=2;}
            else break;
        }
    }
    cout<<ans<<endl;
    return 0;
}

### Error querying database. Cause: java.sql.SQLSyntaxErrorException: Table 't' from one of the SELECTs cannot be used in field list ### The error may exist in com/huawei/eco/product/infrastructure/dao/ProductTemplateMapper.xml ### The error may involve defaultParameterMap ### The error occurred while setting parameters ### SQL: SELECT t.pid deviceId, pc.id productOid, t.template_name templateName, t.template_type templateType, pc.linkage linkage, pc.access_way accessWay, pc.name productName, pc.brand_name brandName, pc.brand_name_en brandNameEn, pc.series productSeries, pc.model productModule, pc.hot_line hotLine, pc.is_serial isSerial, c.icon_url iconUrl, pc.serial_list serialInfoVoList, pc.nan_type simpleConnect, b.communication_module_info moduleName, b.connection_function connectionFunction, b.interaction interaction, b.operating_system operateSystem FROM product_template_info t left join ceco_classification c on t.classification_id=c.oid left join product_core pc on pc.pid=t.pid left join partner p on t.template_creator=p.oid left join product_brace b on pc.id=b.core_id where c.type_id = ? and t.mark_for_delete = 0 and pc.deleted=0 and t.template_creator=? and pc.owner_id=? UNION SELECT t.pid deviceId, pc.id productOid, t.template_name templateName, t.template_type templateType, pc.linkage linkage, pc.access_way accessWay, pc.name productName, pc.brand_name brandName, pc.brand_name_en brandNameEn, pc.series productSeries, pc.model productModule, pc.hot_line hotLine, pc.is_serial isSerial, c.icon_url iconUrl, pc.serial_list serialInfoVoList, pc.nan_type simpleConnect, b.communication_module_info moduleName, b.connection_function connectionFunction, b.interaction interaction, b.operating_system operatingSystem FROM product_template_info t LEFT JOIN ceco_classification c ON t.classification_id = c.oid LEFT JOIN product_core pc ON pc.pid = t.pid LEFT JOIN product_brace b ON pc.id = b.core_id WHERE c.type_id = ? AND t.template_type = 1 AND t.mark_for_delete = 0 AND pc.deleted = 0 order by t.template_type ASC ### Cause: java.sql.SQLSyntaxErrorException: Table 't' from one of the SELECTs cannot be used in field list ; bad SQL grammar []解释
10-22
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值