zoukankan      html  css  js  c++  java
  • VK Cup 2015

    题目链接: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的左边为公共部分。



    代码如下:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef long long LL;
     4 const double eps = 1e-6;
     5 const int INF = 2e9;
     6 const LL LNF = 9e18;
     7 const int mod = 1e9+7;
     8 const int maxn = 1e5+10;
     9 
    10 int n;
    11 char S[maxn], T[maxn];
    12 
    13 int f(char *s1, char *s2, int i, int j)
    14 {
    15     while(i<j && s1[i]==s2[i+1])
    16         i++;
    17     return i==j;
    18 }
    19 
    20 int main()
    21 {
    22     scanf("%d%s%s",&n, S+1,T+1);
    23 
    24     int i = 1, j = n;
    25     while(i<=n && S[i]==T[i]) i++;
    26     while(j>=1 && S[j]==T[j]) j--;
    27 
    28     int ans = 0;
    29     ans += f(S, T, i, j);
    30     ans += f(T, S, i, j);
    31     cout<<ans<<endl;
    32 }
    View Code
  • 相关阅读:
    Android Action Bar简介
    Android UX & UI 最佳实践: 设计有效的导航
    Android Design Principles
    Android 4.4 KitKat 新特性
    Android 尺寸单位转换和屏幕适配相关
    Android UI线程和非UI线程
    初探机器学习之使用百度EasyDL定制化模型
    初探机器学习之使用百度AI服务实现图片识别与相似图片
    初探机器学习之推荐系统的基础知识
    熊逸《唐诗50讲》壮心篇
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7538667.html
Copyright © 2011-2022 走看看