B. Delete from the Left

题目链接

Codeforces Round #496 (Div. 3)--B. Delete from the Left

题解

简单题。题意大概是给定两个字符串,从头以此删去字符,求最少删去多少个字符使得两个字符串相同。
直接从后往前判断有多少对相等的用字符串长度减去即可。

代码

#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
using namespace std;
int n, q1, q2;
string s1, s2;
int main() {
    cin >> s1 >> s2;
    for (int i = 1; i <= min(s1.size(), s2.size()); i++) {
        if (s1[s1.size() - i] == s2[s2.size() - i])n = i;
        else break;
    }
    cout << s1.size() + s2.size() - n - n;
}

标签: implementation, brute force

添加新评论