题目描述

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*,ans=len*;
i=len-,j=len-;
while(j>=&&i>=)
{
if(a[i]==b[j]){
i--;
j--;
sum--;
}
else if(a[i]!=b[j]&&i!=len-){
i=len-;
sum=len*;
}
else if(a[i]!=b[j]&&i==len-){
j--;
sum=len*;
}
}
i=len-,j=len-;
swap(a,b);
while(j>=&&i>=)
{
if(a[i]==b[j]){
i--;
j--;
ans--;
}
else if(a[i]!=b[j]&&i!=len-){
i=len-;
ans=len*;
}
else if(a[i]!=b[j]&&i==len-){
j--;
ans=len*;
}
}
cout<<min(ans,sum)<<endl;
}

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

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

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

今天亲自敲了一下

Prefix and Suffix的更多相关文章

  1. [LeetCode] Prefix and Suffix Search 前后缀搜索

    Given many words, words[i] has weight i. Design a class WordFilter that supports one function, WordF ...

  2. [Swift]LeetCode745. 前缀和后缀搜索 | Prefix and Suffix Search

    Given many words, words[i] has weight i. Design a class WordFilter that supports one function, WordF ...

  3. 745. Prefix and Suffix Search 查找最大index的单词

    [抄题]: Given many words, words[i] has weight i. Design a class WordFilter that supports one function, ...

  4. Prefix and Suffix Search

    Given many words, words[i] has weight i. Design a class WordFilter that supports one function, WordF ...

  5. 【leetcode】745. Prefix and Suffix Search

    题目如下: Given many words, words[i] has weight i. Design a class WordFilter that supports one function, ...

  6. <trim>: prefix+prefixOverrides+suffix+suffixOverrides

    <trim prefix="where" prefixOverrides="where" suffixOverrides="and"& ...

  7. SpringMVC-组件分析之视图解析器(prefix,suffix)

    SpringMVC的默认组件都是在DispatcherServlet.properties配置文件中配置的: spring-webmvc->org/springframewrok/web/ser ...

  8. mybatis之<trim prefix="" suffix="" suffixOverrides="" prefixOverrides=""></trim>的含义

    转自:http://blog.csdn.net/qq_33054511/article/details/70490046   <trim prefix="" suffix=& ...

  9. mybatis之<trim prefix="" suffix="" suffixOverrides="" prefixOverrides=""></trim>

    1.<trim prefix="" suffix="" suffixOverrides="" prefixOverrides=&quo ...

随机推荐

  1. 利用mysecureshell搭建sftp服务

    1.下载对应的mysecureshell-1.33-1.x86_64.rpm包 2.安装mysecureshell-1.33-1.x86_64.rpm 3.添加ftp用户 useradd ftp 4. ...

  2. SpringCloud学习之手把手教你用IDEA搭建入门项目(一)

    SpringCloud简单搭建 jdk:1.8开发工具:IDEA注:需要了解springcloud 1.创建最简单的Maven项目 1)开始创建一个新的项目 ​ 2)创建一个空模板的maven项目,用 ...

  3. 图数据库ubentu环境neo4j安装

    1.下载进入官网下载https://neo4j.com/download-center/#releases 2.设置依赖仓库 wget -O - https://debian.neo4j.org/ne ...

  4. (函数)P1149 火柴棒等式

    题解: #include<stdio.h>int a[10]={6,2,5,5,4,5,6,3,7,6};int num(int n){                          ...

  5. 201771010123汪慧和《面向对象程序设计Java》第十一周实验总结

    一.理论部分 1.栈 (1)栈是一种特殊的线性表,是一种后进先出的结构.(2)栈是限定仅在表尾进行插入和删除运算的线性表,表尾称为栈顶,表头称为栈底.(3)栈的物理存储可以用顺序存储结构,也可以用链式 ...

  6. 视觉slam十四讲个人理解(ch7视觉里程计1)

    参考博文::https://blog.csdn.net/david_han008/article/details/53560736 https://blog.csdn.net/n66040927/ar ...

  7. 内存管理之堆heap

    1.什么是堆? 堆(heap)是一种内存管理方式.内存管理对操作系统来说是一件非常复杂的事情,因为首先内存容量很大, 其次就是内存需求在时间和大小块上没有规律(操作系统上运行着几十甚至几百个进程,这些 ...

  8. centos挂载磁盘

    Aliyun实例为例 简单操作: 查看磁盘情况:fdisk -l 对数据盘进行分区,一般类似/dev/vdb这种为数据盘 输入fdisk  /dev/vdb 对数据盘进行分区.根据提示,输入 n, p ...

  9. JavaScript数组打平(4种方法)

    let arr = [1, 2, [3, 4, 5, [6, 7, 8], 9], 10, [11, 12]]; flatten1 = arr => arr.flat(Infinity) fla ...

  10. JavaScript 之 DOM 与 BOM

    DOM是文档对象模型,用来获取或设置文档中标签的属性,例如获取或者设置input表单的value值. 由于DOM的操作对象是文档(Document),所以dom和浏览器没有直接关系. BOM是浏览器对 ...