codeforces 766A. Mahmoud and Longest Uncommon Subsequence

题目如下
A. Mahmoud and Longest Uncommon Subsequence
time limit per test2 seconds
memory limit per test256 megabytes
While Mahmoud and Ehab were practicing for IOI, they found a problem which name was Longest common subsequence. They solved it, and then Ehab challenged Mahmoud with another problem.

Given two strings a and b, find the length of their longest uncommon subsequence, which is the longest string that is a subsequence of one of them and not a subsequence of the other.

A subsequence of some string is a sequence of characters that appears in the same order in the string, The appearances don't have to be consecutive, for example, strings "ac", "bc", "abc" and "a" are subsequences of string "abc" while strings "abbc" and "acb" are not. The empty string is a subsequence of any string. Any string is a subsequence of itself.

Input
The first line contains string a, and the second line — string b. Both of these strings are non-empty and consist of lowercase letters of English alphabet. The length of each string is not bigger than 105 characters.

Output
If there's no uncommon subsequence, print "-1". Otherwise print the length of the longest uncommon subsequence of an and b.
题目大意
题目要求比较两个字符串,求出两个字符串不重合部分的最大长度是多少

题目分析
以样例为例
abcd
defgh
两个字符串哪怕有重合的部分,但是最长的不重合部分一定是更长的字符串本身的长度,所以
只要比较两个字符串是否完全一致,如果不那么取更长的字符串长度
完整代码如

点击查看代码
#include <stdio.h>
#include <string.h>
 
int main(){
    char a[100005],b[100005];
    scanf("%s%s",a ,b);
    if(strcmp(a, b) == 0) {
        printf("-1\n");
    }else{
        if(strlen(a) > strlen(b)){
            printf("%d",strlen(a));
        }else{
            printf("%d",strlen(b));
        }
    }
    return 0;
}
posted @ 2025-07-02 21:21  sirro1uta  阅读(4)  评论(0)    收藏  举报