题目描述:

最长不重复子串就是从一个字符串中找到一个连续子串,该子串中任何两个字符都不能相同,且该子串的长度是最大的。

输入:

输入包含多个测试用例,每组测试用例输入一行由小写英文字符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 ;
}

考虑用尺取法解此题会不会更快一些?

最新文章

  1. cf#382div2
  2. 安装JBPM6运行环境(JBPM6学习之二)
  3. http://jingyan.baidu.com/article/2009576193ee38cb0721b416.html
  4. viewPager的基本使用
  5. SQL表值函数和标量值函数的区别
  6. UIView 周围出现黑线的解决方法
  7. RPC介绍
  8. Tkinter教程之Event篇(1)&#39;
  9. start.s中的.balignl 16,0xdeadbeef
  10. gcc -lpthread 干什么用
  11. MongoDB理解
  12. matlab函数interp2及其c++代码
  13. 【静默】Oracle各类响应文件何在?
  14. scrapy-pipeline的方法
  15. WebRTC服务器——Licode 环境搭建
  16. SCCM2012 R2实战系列之四:初始化配置
  17. 〖Linux〗Kubuntu文件管理器单例的设置(即:一个工作区只一个文件管理器)
  18. ZooKeeper 未授权访问漏洞
  19. 170822、解决PLSQL记录被另一个用户锁住的问题
  20. golang martini 源码阅读笔记之inject

热门文章

  1. CString的GetBuffer和ReleaseBuffer
  2. mysql IF语句使用
  3. [习题]输入自己的生日(年/月/日)#2 -- 日历(Calendar)控件的时光跳跃,一次跳回五年、十年前?--TodaysDate属性、VisibleDate属性
  4. 使用python查询天气
  5. 几个不错的APP网站。
  6. Bellman-Ford与SPFA
  7. Fiddler模拟POST请求
  8. 团队作业-Beta冲刺第一天
  9. Freemarker入门小案例(生成静态网页的其中一种方式)
  10. Repbase library|divergence rate|self-sequence alignment|genomic rearrangement|cutoffs|breakpoint