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 ...
随机推荐
- 洛谷 P5662 纪念品
题目传送门 解题思路: 一道DP,完全背包,不过有点不同于模板.因为本题的每件物品可自由在不同的时间买卖,且不同时间价格不同. 这道题的关键在于要明白一个非常傻逼的性质,就是我在某天买了第i个物品,然 ...
- 干货 | VPC之间的网络连通实践
随着公有云技术的日臻完善,越来越多的政府部门.事业单位.企业.个人将自己的IT系统部署在公有云之上.在公有云之上部署业务系统有一个特点,就是先要规划网络,有了网络以后,才能把一些相关的产品部署在网络里 ...
- Git 报错:Updates were rejected because the tip of your current branch is behind
刚开始学习 git 命令,发现会出现很多的错误,所以就总结下出现的错误,以此来加深理解和掌握吧! 环境:在本地库操作了一系列的 add 和 commit 操作后,想把本地仓库推送到远端,但是发生以下错 ...
- postman问题
问题1:postman请求出现 'SSL certificate verification' ,实际为https的问题 解决: 1)关闭fiddler 2)或在File > Settings & ...
- python pandas写入excel文件
pandas读取.写入csv数据非常方便,但是有时希望通过excel画个简单的图表看一下数据质量.变化趋势并保存,这时候csv格式的数据就略显不便,因此尝试直接将数据写入excel文件. pandas ...
- 完整注册登陆php源码,附带session验证。
1.首先先写表单页面login.html. <!DOCTYPE html> <html lang="en"> <head> <me ...
- php错误和异常的重定向
通过重定向错误或异常,我们可以更安全的显示错误信息,一般也用来记录错误和异常日志. 参数可以是全局函数名,也可以是类中的方法,非静态方法通过数组传递类名和方法名进去, 静态方法直接带命名空间和类名,看 ...
- 函数返回值return
#函数后面如果没有return系统会默认return none def ff(): print("打印return") return 15 # 函数在执行中遇到return就会停止 ...
- 67)vector的begin() end() 和 front() back()的区别 rbegin() rend()
1) ·············· 2)`````````v1.begin() 和v1.end() 是作为迭代器v1的 第一个位置 和 最后一个元素的下一个位置. `````````````v1. ...
- hasura graphql-engine v1.2.0 beta 版本
hasura graphql-engine v1.2.0 提供了一个很不错的功能action,这个也是目前其他graphql 没有hasura 强大的 地方,使用action 我们可以更好的扩展has ...