跳到正文

题解

AT_abc280_c 题解

shinzAnmono约 1 分钟
原文
文章信息与版本

文章信息

发布
2022年12月6日
收录
2025年12月12日
更新
2026年11月20日
篇幅
约 1 分钟

版本

  1. v1当前

题意:

给定字符串 SS,TT,请求出两者第一个不同的字符的位置。

Solution:

由于字符串的长度很短,所以我们可以枚举较短的字符串每个字符,直到找到不同的字符为止,并输出这个字符的位置。

如果较短的字符串中并未找到不同的字符,那么这个字符一定在较长的字符串的末尾。

Code:

C++
#include<iostream>
#include<algorithm>
#include<string>
int main() {
    std::ios::sync_with_stdio(false);
    std::string s, t;
    std::cin >> s >> t;
    for (int i = 1; i <= std::min(s.length(), t.length()); i++)
        if (s[i - 1] != t[i - 1]) std::cout << i, exit(0);
    std::cout << std::max(s.length(), t.length());
    return 0;
}
原文