题目链接: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. 最全linux命令

    arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIOS / DMI ...

  2. Android破解学习之路(五)——Android游戏 割绳子:魔法 + 在游戏加入Toast弹窗提示

    前言:这一期的破解教程,有新的知识内容出现啦! 这一期破解的游戏是找不到之前的关键字,怎么破解呢? 破解成功之后,添加一个Toast弹窗提示由XX破解,这操作该如何实现呢?请往下看~ 链接: http ...

  3. Webservice接口的调用

    一.开发webservice接口的方式 1.jdk开发. 2.使用第三方工具开发,如cxf.shiro等等. 我这边介绍jdk方式webservice接口调用. 二.使用jdk调用webservice ...

  4. Webpack 2 视频教程 020 - Webpack 2 中的 HMR ( Hot Module Replacement )

    原文发表于我的技术博客 这是我免费发布的高质量超清「Webpack 2 视频教程」. Webpack 作为目前前端开发必备的框架,Webpack 发布了 2.0 版本,此视频就是基于 2.0 的版本讲 ...

  5. docker:(1)docker基本命令使用及发布镜像

    docker镜像可以完全看作一台全新的电脑使用,无论什么镜像都是对某一东西进行了配置,然后打包后可以快速移植到需要的地方直接使用 省去复杂的配置工作 比如java web项目部署,如果是新部署,需要装 ...

  6. SQL 多列合并一列

    select rtrim(姓)+ rtrim(名) as 姓名 from tb

  7. python pandas stack和unstack函数

    在用pandas进行数据重排时,经常用到stack和unstack两个函数.stack的意思是堆叠,堆积,unstack即"不要堆叠",我对两个函数是这样理解和区分的. 常见的数据 ...

  8. jQuery基础 (四)——使用jquery-cookie 实现点赞功能

    jquery-cookie 下载地址:https://github.com/carhartl/jquery-cookie 直接上代码 html <span class="jieda-z ...

  9. PXE+kickstart网络安装CentOS7.4系统及过程中各种报错

    环境:关闭防火墙.selinux 注意:虚拟机进行网络安装的话,7.3以后的系统是需要2G以上的内存 [root@kickstart ~]# cat /etc/redhat-release CentO ...

  10. cobbler自动安装系统(Centos7.X)

    环境: [root@kickstart ~]# cat /etc/redhat-release CentOS Linux release (Core) [root@kickstart ~]# unam ...