VK Cup 2015

来源:互联网 发布:制作班徽用什么软件 编辑:程序博客网 时间:2024/04/23 17:09

题目链接: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
7readingtrading
output
1
input
5sweetsheep
output
0
input
3toytry
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;}