VK Cup 2015 - Round 2 E. Correcting Mistakes —— 字符串

本文解析了CodeForces平台上E题“Correcting Mistakes”的解题思路与代码实现。探讨了如何判断两个长度相同的单词是否能通过删除一个字母从同一个原始单词得到,并给出了高效的算法解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://codeforces.com/contest/533/problem/E


E. Correcting Mistakes
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Analyzing the mistakes people make while typing search queries is a complex and an interesting work. As there is no guaranteed way to determine what the user originally meant by typing some query, we have to use different sorts of heuristics.

Polycarp needed to write a code that could, given two words, check whether they could have been obtained from the same word as a result of typos. Polycarpus suggested that the most common typo is skipping exactly one letter as you type a word.

Implement a program that can, given two distinct words S and T of the same length n determine how many words W of length n + 1 are there with such property that you can transform W into both S, and T by deleting exactly one character. Words S and T consist of lowercase English letters. Word W also should consist of lowercase English letters.

Input

The first line contains integer n (1 ≤ n ≤ 100 000) — the length of words S and T.

The second line contains word S.

The third line contains word T.

Words S and T consist of lowercase English letters. It is guaranteed that S and T are distinct words.

Output

Print a single integer — the number of distinct words W that can be transformed to S and T due to a typo.

Examples
input
7
reading
trading
output
1
input
5
sweet
sheep
output
0
input
3
toy
try
output
2
Note

In the first sample test the two given words could be obtained only from word "treading" (the deleted letters are marked in bold).

In the second sample test the two given words couldn't be obtained from the same word by removing one letter.

In the third sample test the two given words could be obtained from either word "tory" or word "troy".




题解:

假设字符串为S和T,可知答案最大只能为2.

1.跳过左右两端相同的部分,直到遇到相等的字符为止。

2.只有两种情况:S的左边和T的右边为公共部分、S的右边和T的左边为公共部分。



代码如下:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const double eps = 1e-6;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+7;
const int maxn = 1e5+10;

int n;
char S[maxn], T[maxn];

int f(char *s1, char *s2, int i, int j)
{
    while(i<j && s1[i]==s2[i+1])
        i++;
    return i==j;
}

int main()
{
    scanf("%d%s%s",&n, S+1,T+1);

    int i = 1, j = n;
    while(i<=n && S[i]==T[i]) i++;
    while(j>=1 && S[j]==T[j]) j--;

    int ans = 0;
    ans += f(S, T, i, j);
    ans += f(T, S, i, j);
    cout<<ans<<endl;
}


[INFO] lxe-common-datascope ............................... SUCCESS [ 0.388 s] [INFO] lxe-common-log ..................................... SUCCESS [ 0.439 s] [INFO] lxe-common-swagger ................................. SUCCESS [ 0.465 s] [INFO] lxe-api-transport .................................. SUCCESS [ 0.426 s] [INFO] lxe-api-order ...................................... SUCCESS [ 0.431 s] [INFO] lxe-api-queue ...................................... SUCCESS [ 0.417 s] [INFO] lxe-api-security ................................... SUCCESS [ 0.360 s] [INFO] lxe-common-eqb ..................................... SUCCESS [ 0.582 s] [INFO] lxe-modules ........................................ SUCCESS [ 0.020 s] [INFO] lxe-modules-system ................................. FAILURE [ 1.565 s] [INFO] lxe-modules-file ................................... SKIPPED [INFO] lxe-modules-transport .............................. SKIPPED [INFO] lxe-modules-goods .................................. SKIPPED [INFO] lxe-api-goods ...................................... SKIPPED [INFO] lxe-api-finance .................................... SKIPPED [INFO] lxe-api-dispatch ................................... SKIPPED [INFO] lxe-modules-order .................................. SKIPPED [INFO] lxe-api-maintenance ................................ SKIPPED [INFO] lxe-modules-dispatch ............................... SKIPPED [INFO] lxe-modules-queue .................................. SKIPPED [INFO] lxe-modules-finance ................................ SKIPPED [INFO] lxe-modules-websocket .............................. SKIPPED [INFO] lxe-modules-statistics ............................. SKIPPED [INFO] lxe-modules-officialweb ............................ SKIPPED [INFO] lxe-common-wjy ..................................... SKIPPED [INFO] lxe-api-account .................................... SKIPPED [INFO] lxe-modules-maintenance ............................ SKIPPED [INFO] lxe-modules-account ................................ SKIPPED [INFO] lxe-modules-secuirty ............................... SKIPPED [INFO] lxe-common-seata ................................... SKIPPED [INFO] lxe-common-zjxl .................................... SKIPPED [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 24.105 s [INFO] Finished at: 2025-07-28T17:40:33+08:00 [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal on project lxe-modules-system: Could not resolve dependencies for project com.lxe:lxe-modules-system:jar:3.6.1: Could not find artifact com.zhongjiaoxinglu:zhongjiaoxinglu-openapi-sdk:jar:6.0 in pu blic (https://maven.aliyun.com/repository/public) -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException [ERROR] [ERROR] After correcting the problems, you can resume the build with the command [ERROR] mvn <args> -rf :lxe-modules-system 报错
最新发布
07-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值