Prefix and Suffix
题目描述
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.
输入
N
s
t
输出
样例输入
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的更多相关文章
- [LeetCode] Prefix and Suffix Search 前后缀搜索
Given many words, words[i] has weight i. Design a class WordFilter that supports one function, WordF ...
- [Swift]LeetCode745. 前缀和后缀搜索 | Prefix and Suffix Search
Given many words, words[i] has weight i. Design a class WordFilter that supports one function, WordF ...
- 745. Prefix and Suffix Search 查找最大index的单词
[抄题]: Given many words, words[i] has weight i. Design a class WordFilter that supports one function, ...
- Prefix and Suffix Search
Given many words, words[i] has weight i. Design a class WordFilter that supports one function, WordF ...
- 【leetcode】745. Prefix and Suffix Search
题目如下: Given many words, words[i] has weight i. Design a class WordFilter that supports one function, ...
- <trim>: prefix+prefixOverrides+suffix+suffixOverrides
<trim prefix="where" prefixOverrides="where" suffixOverrides="and"& ...
- SpringMVC-组件分析之视图解析器(prefix,suffix)
SpringMVC的默认组件都是在DispatcherServlet.properties配置文件中配置的: spring-webmvc->org/springframewrok/web/ser ...
- mybatis之<trim prefix="" suffix="" suffixOverrides="" prefixOverrides=""></trim>的含义
转自:http://blog.csdn.net/qq_33054511/article/details/70490046 <trim prefix="" suffix=& ...
- mybatis之<trim prefix="" suffix="" suffixOverrides="" prefixOverrides=""></trim>
1.<trim prefix="" suffix="" suffixOverrides="" prefixOverrides=&quo ...
随机推荐
- VBA代码优化及其他设置操作
一.代码优化的一些方法 尽量减少在循环中遍历调用对象,公式计算 (操作VBA代码若出现屏幕闪屏,会拖慢运行速度),可以禁止屏幕闪屏.多用在操作工作表/薄,单元格的时候. Application.Scr ...
- linux 下shell 编写脚本
linux 下shell 编写脚本: 1.程序结构练习:编写一个脚本,给定一个正整数,计算出这个数所有位的数字之和. 例如:程序给定输入123,那么应该返回1+2+3=6. 2.程序结构练习:编写一个 ...
- 创建简单spring boot项目
简介 使用spring boot可以轻松创建独立的,基于Spring框架的生产级别应用程序.Spring boot应用程序只需要很少的spring配置 特点 创建独立的Spring应用程序 直接嵌入t ...
- Java中Scanner类在nextInt()后无法输入nextLine()的问题
首先,Scanner是一个扫描器,它扫描数据都是去内存中一块缓冲区中进行扫描并读入数据的,而我们在控制台中输入的数据也都是被先存入缓冲区中等待扫描器的扫描读取.这个扫描器在扫描过程中判断停止的依据就是 ...
- 京东云携手Mellanox,设计最先进SDN硬件加速功能并开源
京东云携手Mellanox,设计最先进SDN硬件加速功能并开源 最新技术播报 京东云开发者社区 导语新一代 SDN.NFV 和云原生计算技术正在推动应用实例的极限,这些实例可以在虚拟化和容器化的服务 ...
- UML-设计模式-本地服务容错-适配器+工厂模式
问题1:我们的ProductCatalog存储在了数据库里了,但是数据库瘫掉了,怎么办? 解决:本地(Map)---->Local(文件)---->DB 问题2:如果新加了存储Produc ...
- 设x,y是概率空间(Ω,F,P)上的拟可积随机变量,证明:X=Y a.e 当且仅当 xdp = ydp 对每个A∈F成立。Q: X=Y almost surely iff ∀A∈G∫AXdP=∫AYdP
E{XE{Y|C}}=E{YE{X|C}} 现在有没有适合大学生用的搜题软件呢? https://www.zhihu.com/question/51935291/answer/514312093 ...
- Unity3D事件函数的执行顺序
In Unity scripting, there are a number of event functions that get executed in a predetermined order ...
- Java架构师笔记-你必须掌握的 21 个 Java 核心技术!(干货)
闲来无事,师长一向不(没)喜(有)欢(钱)凑热闹,倒不如趁着这时候复盘复盘.而写这篇文章的目的是想总结一下自己这么多年来使用java的一些心得体会,希望可以给大家一些经验,能让大家更好学习和使用Jav ...
- 数组,字符串方法总结 Unicode 数字
String.prototype.charCodeAt(index) 就是返回字符串中下标单个数值 对应的编码表的10进制表示数值 方法返回0到65535之间的整数,表示给定索引处的UTF-16代码 ...