题目链接: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的题、
 #include<cstring>
#include <cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int N = ;
char s[];
int mp[N];
int main()
{
int n,l,r,curL, len; //l, r 是最终答案
while(~scanf("%d",&n))
{
getchar();
gets(s);
len = ;
curL = ;
memset(mp,-,sizeof(mp));
for(int i = ; i < n; i++)
{
if(mp[s[i]] >= curL)
{
if(i-curL > len)
{
len = i-curL;
l = curL, r = i-;
}
curL = mp[s[i]]+;
}
mp[s[i]] = i;
}
if(n-curL > len) {
len = n-curL;
l = curL, r = n-;
}
printf("%d %d %d\n", len, l, r);
}
return ;
}

下面是超时代码:

 #include<cstdio>
#include<cstring>
using namespace std;
const int N = ;
int vis[N]; int main()
{
int n;
while(~scanf("%d",&n))
{
memset(vis,-,sizeof(vis));
char ch;
getchar();
int l=, r=-;
int len = ;
int ansl = ;
int ansr = -;
int anslen = ;
for(int i = ; i < n; i++)
{
scanf("%c",&ch);
int tm = ch-'a';
//printf("tm = %d\n",tm);
r++;
len++;
if(vis[tm]==-){
//vis[ch] = i;
if(len>anslen){
ansl = l;
ansr = r;
anslen = len;
}
}
while(vis[tm]!=-){
for(int j = ; j < ; j++)
{
if(vis[j]==l) {
vis[j] = -;
l++;
len--;
break;
}
}
if(len>anslen){
ansl = l;
ansr = r;
anslen = len;
}
}
vis[tm] = i;
}
printf("%d %d %d\n",anslen,ansl,ansr);
}
return ;
}

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. laravel and lumen 软删除操作

    知识都是有联系的,这绝对是真理.作为一名小白,看了一点官方文档,把我自己理解的软删除操作给大家讲讲.有些就是套用官方文档的话. 定义:什么是软删除呢,所谓软删除指的是数据表记录并未真的从数据库删除,而 ...

  2. redis中使用 check-and-set 操作实现乐观锁

    WATCH 命令可以为 Redis 事务提供 check-and-set (CAS)行为. 被 WATCH 的键会被监视,并会发觉这些键是否被改动过了. 如果有至少一个被监视的键在 EXEC 执行之前 ...

  3. Selinux安全机制

    1.Selinux安全机制简介 Selinux是Google在Android 4.4上正式推出的一套以SELinux为基础于核心的系统安全机制.而SELinux则是由美国NSA(国安局)和一些公司(R ...

  4. lesson - 2 笔记 yum /single /rescue /

    一. yum  作用:                     yum 命令是在Fedora 和RedHat 以及SUSE 中基于rpm 的软件包管理器,它可以使系统管理人员交互和自动化地更新与管理R ...

  5. MySQL 最左前缀(Leftmost Prefix) & 组合索引(复合索引,多列索引)

    资料来源于网络,仅供参考学习. CREATE TABLE test(a INT,b INT,c INT,KEY idx(a,b,c)); 优: SELECT * FROM test WHERE a=1 ...

  6. Golang 中的坑 一

    Golang 中的坑 短变量声明  Short variable declarations 考虑如下代码: package main import ( "errors" " ...

  7. 记录优雅的pythonic代码

    记录平时学习中接触到的和网上看到的一些pythonic的方法,只为日后查询时候方便. 1.列表推导式: seq_list=[1,2,3,4,5] new_list=[i *2 for i in seq ...

  8. SVN添加用户权限

    点击properties

  9. haslayout知多少

    我们都知道浏览器有bug,而IE的bug似乎比大多数浏览器都多.IE的表现与其他浏览器不同的原因之一就是,显示引擎使用一个称为布局(layout)的内部概念.   因为布局是专门针对显示引擎内部工作方 ...

  10. 例子:js超级玛丽小游戏

    韩顺平_轻松搞定网页设计(html+css+javascript)_第34讲_js超级玛丽小游戏_学习笔记_源代码图解_PPT文档整理 采用面向对象思想设计超级马里奥游戏人物(示意图) 怎么用通过按键 ...