题目描述

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. jquery的读、写、增、删、查方法

    # 注:jquery需要导包  格式<script type="text/javascript" src="jquery-3.2.1.js">> ...

  2. MySQL 插入 中文数据乱码解决

    问题描述: 1.在命令行中进行插入,没有问题.但是显示存在部分乱码 2.在JDBC中插入成功.中文是直接以“??”形式显示. 通过Navicat客户端查看 与在网页中看到的一一致,说明读取没有问题,问 ...

  3. swoole怎么连接数据库

    异步连接到MySQL服务器. $serverConfig为MySQL服务器的配置,必须为关联索引数组 $callback连接完成后回调此函数 swoole连接数据库实例: 推荐学习:swoole教程 ...

  4. delphi 文本 记录 流式 读写文件

    unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...

  5. GBDT入门

    GBDT(MART)迭代决策树入门教程 GBDT(Gradient Boosting Decision Tree) 又叫 MART(Multiple Additive Regression Tree) ...

  6. javaweb04 ServletRequest&ServletResponse

    WEB浏览器与WEB服务器之间的一问一答的交互过程必须遵循一定的规则,这个歌规则就是 HTTP协议HTTP协议是超文本传输协议,它是TCP/IP协议集中的一个应用层协议,用于定义WEB浏览器与WEB服 ...

  7. 将微服务注册到Euraka

    1.添加依赖 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId> ...

  8. php错误和异常的重定向

    通过重定向错误或异常,我们可以更安全的显示错误信息,一般也用来记录错误和异常日志. 参数可以是全局函数名,也可以是类中的方法,非静态方法通过数组传递类名和方法名进去, 静态方法直接带命名空间和类名,看 ...

  9. JS变量、作用域及内存

    1.动态属性var box = new Object();box.name = 'lee';alert(box.name); var box = 'lee';box.age = '28';alert( ...

  10. ACM-ICPC Asia Beijing Regional Contest 2018 Reproduction hihocoder1870~1879

    ACM-ICPC Asia Beijing Regional Contest 2018 Reproduction hihocoder1870~1879 A 签到,dfs 或者 floyd 都行. #i ...