九度oj 题目1530:最长不重复子串
2024-08-29 19:57:54
- 题目描述:
-
最长不重复子串就是从一个字符串中找到一个连续子串,该子串中任何两个字符都不能相同,且该子串的长度是最大的。
- 输入:
-
输入包含多个测试用例,每组测试用例输入一行由小写英文字符a,b,c...x,y,z组成的字符串,字符串的长度不大于10000。
- 输出:
-
对于每组测试用例,输出最大长度的不重复子串长度。
- 样例输入:
-
absd
abba
abdffd
- 样例输出:
-
4
2
4
一开始的代码是这样#include <cstdio>
#include <cstdlib>
#include <cstring> char toDo[];
int cnt[]; int main(int argc, char const *argv[])
{
//freopen("input.txt","r",stdin);
while(scanf("%s",toDo) != EOF) {
int max = ;
memset(cnt, , sizeof(cnt));
cnt[] = ;
for(int i = ; toDo[i]; i++) {
int temp = ;
for(int j = i-; j >= i - cnt[i-]; j--) {
if(toDo[j] == toDo[i]) {
break;
}
else {
temp++;
}
}
cnt[i] = temp;
if(max < cnt[i]) {
max = cnt[i];
}
}
printf("%d\n",max);
}
return ;
}后来发现不需要cnt[],只需记住上一个cnt即可,代码如下
#include <cstdio>
char toDo[];
int main(int argc, char const *argv[])
{
while(scanf("%s",toDo) != EOF) {
int max = ;
int cntl = ;
for(int i = ; toDo[i]; i++) {
int temp = ;
for(int j = i-; j >= i - cntl; j--) {
if(toDo[j] == toDo[i]) {
break;
}
else {
temp++;
}
}
if(max < temp) {
max = temp;
}
cntl = temp;
}
printf("%d\n",max);
}
return ;
}考虑用尺取法解此题会不会更快一些?
最新文章
- cf#382div2
- 安装JBPM6运行环境(JBPM6学习之二)
- http://jingyan.baidu.com/article/2009576193ee38cb0721b416.html
- viewPager的基本使用
- SQL表值函数和标量值函数的区别
- UIView 周围出现黑线的解决方法
- RPC介绍
- Tkinter教程之Event篇(1)&#39;
- start.s中的.balignl 16,0xdeadbeef
- gcc -lpthread 干什么用
- MongoDB理解
- matlab函数interp2及其c++代码
- 【静默】Oracle各类响应文件何在?
- scrapy-pipeline的方法
- WebRTC服务器——Licode 环境搭建
- SCCM2012 R2实战系列之四:初始化配置
- 〖Linux〗Kubuntu文件管理器单例的设置(即:一个工作区只一个文件管理器)
- ZooKeeper 未授权访问漏洞
- 170822、解决PLSQL记录被另一个用户锁住的问题
- golang martini 源码阅读笔记之inject
热门文章
- CString的GetBuffer和ReleaseBuffer
- mysql IF语句使用
- [习题]输入自己的生日(年/月/日)#2 -- 日历(Calendar)控件的时光跳跃,一次跳回五年、十年前?--TodaysDate属性、VisibleDate属性
- 使用python查询天气
- 几个不错的APP网站。
- Bellman-Ford与SPFA
- Fiddler模拟POST请求
- 团队作业-Beta冲刺第一天
- Freemarker入门小案例(生成静态网页的其中一种方式)
- Repbase library|divergence rate|self-sequence alignment|genomic rearrangement|cutoffs|breakpoint