zoukankan      html  css  js  c++  java
  • Prefix and Suffix

    题目描述

    Snuke is interested in strings that satisfy the following conditions:

    The length of the string is at least N.
    The first N characters equal to the string s.
    The last N characters equal to the string t.
    Find the length of the shortest string that satisfies the conditions.

    Constraints
    1≤N≤100
    The lengths of s and t are both N.
    s and t consist of lowercase English letters.

    输入

    The input is given from Standard Input in the following format:

    N
    s
    t

    输出

    Print the length of the shortest string that satisfies the conditions.

    样例输入

    3
    abc
    cde
    

    样例输出

    5
    

    提示

    The shortest string is 'abcde'.

    #include <bits/stdc++.h>
    
    using namespace std;
    int main(){
        ios::sync_with_stdio(false);
        int len;
        string a,b;
        cin>>len;
        cin>>a>>b;
        int i,j,sum=len*2,ans=len*2;
        i=len-1,j=len-1;
        while(j>=0&&i>=0)
        {
            if(a[i]==b[j]){
                i--;
                j--;
                sum--;
            }
            else if(a[i]!=b[j]&&i!=len-1){
                i=len-1;
                sum=len*2;
            }
            else if(a[i]!=b[j]&&i==len-1){
                j--;
                sum=len*2;
            }
        }
        i=len-1,j=len-1;
        swap(a,b);
        while(j>=0&&i>=0)
        {
            if(a[i]==b[j]){
                i--;
                j--;
                ans--;
            }
            else if(a[i]!=b[j]&&i!=len-1){
                i=len-1;
                ans=len*2;
            }
            else if(a[i]!=b[j]&&i==len-1){
                j--;
                ans=len*2;
            }
        }
        cout<<min(ans,sum)<<endl;
    }
    View Code

    这道题刚开始就WA了一大片

    这个不能天真的以为b接在a后面

    还有天天吃小白菜的代码真的太乱了

    今天亲自敲了一下

    不要忘记努力,不要辜负自己 欢迎指正 QQ:1468580561
  • 相关阅读:
    微信报警提示
    使用pygal图表显示网站API接口数据
    读写文本文件,乱码解决方案
    MD5加密
    将DataTable导入到SQL数据库表中
    NPOI组件操作Excel导入、导出
    二叉树由先序和中序建树
    用两个栈模拟队列
    math type白嫖教程
    IDEA常用快捷键
  • 原文地址:https://www.cnblogs.com/smallocean/p/8821257.html
Copyright © 2011-2022 走看看