练习cf2025A. Two Screens

题目如下
A. Two Screens
https://codeforces.com/problemset/problem/2025/A
time limit per test2 seconds
memory limit per test512 megabytes
There are two screens which can display sequences of uppercase Latin letters. Initially, both screens display nothing.

In one second, you can do one of the following two actions:

choose a screen and an uppercase Latin letter, and append that letter to the end of the sequence displayed on that screen;
choose a screen and copy the sequence from it to the other screen, overwriting the sequence that was displayed on the other screen.
You have to calculate the minimum number of seconds you have to spend so that the first screen displays the sequence 𝑠, and the second screen displays the sequence 𝑡.

Input
The first line contains one integer 𝑞 (1≤𝑞≤500) — the number of test cases.

Each test case consists of two lines. The first line contains the string 𝑠, and the second line contains the string 𝑡 (1≤|𝑠|,|𝑡|≤100). Both strings consist of uppercase Latin letters.

Output
For each test case, print one integer — the minimum possible number of seconds you have to spend so that the first screen displays the sequence 𝑠, and the second screen displays the sequence 𝑡.

题目大意
每组有两串字符,现有两张空白屏幕,任意从一块开始拼写,可以对每次拼写完毕可以将一块屏幕的内容拷贝到另一块(从头开始拷贝),每次拷贝需要1s,每拼写一个字母也需要1s,请问用该方法最短需要多长时间才能拼写完两串字符串。

题目分析
因为题目限制,每次的拷贝都是对当前某一块屏幕全部内容的拷贝,那么意思是只能考虑两串字符串的前m个字符中对应位置是否重合来进行拷贝。
注意每次拷贝消耗1s。
代码如下

点击查看代码
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main(){
    int t;
    cin >> t;
    while(t--){
        string a, b;
        cin >> a >> b;
        int same = 0;
        for(int i = 0; i < a.size(); i++){
            if(a[i] == b[i]){
                same++;
            }else{
                break;
            }
        }
        int cnt = a.size() + b.size() - same;;
        if(same){
            cnt += 1;
        }
        cout << cnt << endl;
    }
    return 0;
}
posted @ 2025-08-08 21:52  sirro1uta  阅读(11)  评论(0)    收藏  举报