题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2668

Daydream

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1658    Accepted Submission(s): 490

Problem Description
Welcome to 2009 HDU Girl’s Cup, bless you will happy in it. Every girl are beautiful if you use you heart to feel. Every corner in the world will colourful and energetic by several girls standing. If you boy, a normal bay, I believe that you will try to watch when a beautiful girl passing you and you will nervous if a girl watching you just when you are watching her. Now give you a surprise that may be never happy in the real time. Millions of PLMM stand in a line and facing you(^_^). They are dress colourful clothings. You should to find out a maximal subline PLMM that their clothing color are all different.
 
Input
The input contains multiple test cases. Each case first give a integer n expressing many many girls stand in line.(n<=10000000) Next line a string including n character, each character standing one girls’s clothing color.
 
Output
Output one integer the numbers of maximal subline PLMM that their clothing color are all different and the line's begin and end (based from 0). If their are more answer, print the begin smallest.
 
Sample Input
3 abc 5 aaaba 8 hdugirls
 
Sample Output
3 0 2 2 2 3 8 0 7
 
Author
yifenfei
 
题意:  O(n)求最长不重复子串,注意,这里的字符没有说是从a~z所以要取所有的ASCII(0~127   因为是7位表示的)设置一个数组mp[127]表示对应字符出现在当前时期的前一个位置是哪里l ,r 表示扫描的左右端点,如果输入的ch 对应的mp[ch]>l 就要修改l的值为mp[ch]+1,这种O(n)的思想要注意的是输入输出容易超时,每次都申请读入是很耗费时间的,所以,开一个10000000的数组,直接用gets(s); 读入即可,下面是我超时的代码和ac的代码,可以看到真的有卡scanf的题、
  1. #include<cstring>
  2. #include <cstdio>
  3. #include<iostream>
  4. #include<algorithm>
  5. using namespace std;
  6. const int N = ;
  7. char s[];
  8. int mp[N];
  9. int main()
  10. {
  11. int n,l,r,curL, len; //l, r 是最终答案
  12. while(~scanf("%d",&n))
  13. {
  14. getchar();
  15. gets(s);
  16. len = ;
  17. curL = ;
  18. memset(mp,-,sizeof(mp));
  19. for(int i = ; i < n; i++)
  20. {
  21. if(mp[s[i]] >= curL)
  22. {
  23. if(i-curL > len)
  24. {
  25. len = i-curL;
  26. l = curL, r = i-;
  27. }
  28. curL = mp[s[i]]+;
  29. }
  30. mp[s[i]] = i;
  31. }
  32. if(n-curL > len) {
  33. len = n-curL;
  34. l = curL, r = n-;
  35. }
  36. printf("%d %d %d\n", len, l, r);
  37. }
  38. return ;
  39. }

下面是超时代码:

  1. #include<cstdio>
  2. #include<cstring>
  3. using namespace std;
  4. const int N = ;
  5. int vis[N];
  6.  
  7. int main()
  8. {
  9. int n;
  10. while(~scanf("%d",&n))
  11. {
  12. memset(vis,-,sizeof(vis));
  13. char ch;
  14. getchar();
  15. int l=, r=-;
  16. int len = ;
  17. int ansl = ;
  18. int ansr = -;
  19. int anslen = ;
  20. for(int i = ; i < n; i++)
  21. {
  22. scanf("%c",&ch);
  23. int tm = ch-'a';
  24. //printf("tm = %d\n",tm);
  25. r++;
  26. len++;
  27. if(vis[tm]==-){
  28. //vis[ch] = i;
  29. if(len>anslen){
  30. ansl = l;
  31. ansr = r;
  32. anslen = len;
  33. }
  34. }
  35. while(vis[tm]!=-){
  36. for(int j = ; j < ; j++)
  37. {
  38. if(vis[j]==l) {
  39. vis[j] = -;
  40. l++;
  41. len--;
  42. break;
  43. }
  44. }
  45. if(len>anslen){
  46. ansl = l;
  47. ansr = r;
  48. anslen = len;
  49. }
  50. }
  51. vis[tm] = i;
  52. }
  53. printf("%d %d %d\n",anslen,ansl,ansr);
  54. }
  55. return ;
  56. }

hdu_2668 Daydream O(n)求最长不重复子串的更多相关文章

  1. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  2. hdu 3068 最长回文(manachar求最长回文子串)

    题目连接:hdu 3068 最长回文 解题思路:通过manachar算法求最长回文子串,如果用遍历的话绝对超时. #include <stdio.h> #include <strin ...

  3. PAT甲题题解-1040. Longest Symmetric String (25)-求最长回文子串

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789177.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  4. Manacher模板( 线性求最长回文子串 )

    模板 #include<stdio.h> #include<string.h> #include<algorithm> #include<map> us ...

  5. Longest Substring Without Repeating Characters 最长不重复子串

    只遍历一次字符串即可求出最长不重复子串的长度. int lengthOfLongestSubstring(string s) { vector<,-); //记录字符上一次出现的位置,ASCII ...

  6. LeetCode:Longest Substring Without Repeating Characters(最长不重复子串)

    题目链接 Given a string, find the length of the longest substring without repeating characters. For exam ...

  7. 九度oj 1530 最长不重复子串

    原题链接:http://ac.jobdu.com/problem.php?pid=1530 字符串简单题,看似O(n^2)的复杂度10000的数据量会tle,其实最长不重复子串不超过26个嘛... 如 ...

  8. [Jobdu] 题目1530:最长不重复子串

    题目描述: 最长不重复子串就是从一个字符串中找到一个连续子串,该子串中任何两个字符都不能相同,且该子串的长度是最大的. 输入: 输入包含多个测试用例,每组测试用例输入一行由小写英文字符a,b,c... ...

  9. 基于python 3.5 所做的找出来一个字符串中最长不重复子串算法

    功能:找出来一个字符串中最长不重复子串 def find_longest_no_repeat_substr(one_str): #定义一个列表用于存储非重复字符子串 res_list=[] #获得字符 ...

随机推荐

  1. xCode8以及iOS10 的新特性

    其他:ios10中 适配问题(1.系统判断方法失效:2.隐私数据的访问问题:3.UIColor 问题4.真彩色的显示5.ATS问题6.UIStatusBar问题7.UITextField8.UserN ...

  2. 为什么CPU需要时钟这种概念?

    本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/132 最近在研究计算机里的基本逻辑电路,想到一个问题:为什么CP ...

  3. Java 管程解决生产者消费者问题

    同样是实验存档.//.. 依然以生产者消费者问题作为背景. 管程(=“资源管理程序”)将资源和对资源的操作封装起来,资源使用者通过接口操作资源就 ok,不用去考虑进程同步的问题. 管程: packag ...

  4. touchstart和touchend事件

    touchstart和touchend事件 移动互联网是未来的发展趋势,现在国内很多互联网大佬都在争取移动这一块大饼,如微信及支付宝是目前比较成功的例子,当然还有各种APP和web运用. 由于公司的需 ...

  5. 关于VS2017安装的一点扩充说明(15.5)

    其实逆天不推荐自己慢慢离线,找个离线包更新下再打包更快 Key:http://www.cnblogs.com/dunitian/p/4667038.html VS完整卸载工具:https://gith ...

  6. Fiddler扩展之脚本录制

    Jmeter的脚本来源有4个,此处重点说明第4个 1)手动编写 2)badboy录制 3)自带录制功能 4)Fiddler生成 本文的主要用途:将fiddler抓取的请求,导出为jmx格式,方便jme ...

  7. Xamarin 调用JSON.net来解析JSON 转(Model) json2csharp.com/

    https://www.cnblogs.com/zjoch/p/4458516.html   再来我们要怎么解析JSON格示呢?在.net 中,我们很孰悉的JSON.net,没错,我们依然可以在Xam ...

  8. perl多线程使用

    原文来自:博客园(华夏35度)http://www.cnblogs.com/zhangchaoyang 作者:Orisun <<=========================threa ...

  9. tophat安装

    1     依赖软件:bowtie,bowtie2,samtools,boost c++ library 2     建立索引文件:      bowtie包括bowtie,bowtie-build, ...

  10. Git与GitHub学习笔记(八)git如何同时同步提交到码云和GitHub上

    前言: 今天github push代码一直push不上去,打算就备份一份代码带国内开源码云上. Github容易出现的情况是: 国内访问速度比较慢, 如果被墙掉的话,就直接没发使用了 如果开源个PHP ...