题目链接: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. Kafka+Storm写入Hbase和HDFS

    1.Storm整合Kafka 使用Kafka作为数据源,起到缓冲的作用 // 配置Kafka订阅的Topic,以及zookeeper中数据节点目录和名字 String zks = KafkaPrope ...

  2. [置顶] MVC输出缓存(OutputCache参数详解)

    1.学习之前你应该知道这些 几乎每个项目都会用到缓存,这是必然的.以前在学校时做的网站基本上的一个标准就是1.搞定增删改查2.页面做的不要太差3.能运行(ps真的有这种情况,答辩验收的时候几个人在讲台 ...

  3. openstack操作之一 命令行

    在openstack环境中提供了多种操作虚拟机的方法,有最简单直接的dashborad界面,有不直观但高效的命令行,还有进阶版的postman调用openstack restfulapi和命令行中使用 ...

  4. Thomas Hobbes: Leviathan

    Man is distinguished, not only by his reason, but by this singular passion from other animals, which ...

  5. 关于模拟登陆微博(PC)

    微博模拟登陆 1.基类对象的方法建立一个类__init__初始化方法,接收username和password. class launcher(): def __init__(self, usernam ...

  6. mysql change master导致gtid丢失

    change master导致gtid丢失从innobackupex恢复导致binlog的拉取位置会导致主备gtid不一致.此类错误通过构造空事务方式无法修复.此时就需要change master 方 ...

  7. Android中菜单图标等系统自带的图标

    Android™ 2.1 android.R.drawable Icon Resources Android™ 1.5 android.R.drawable Icon Resources Androi ...

  8. NET控件Designer架构设计

    总体结构 Designer总体上由三大部分组成:View,ViewModel和Model,这个结构借鉴了流行的MVVM模式.这三部分的职责分工是: View 负责把ViewModel以图形的方式展现出 ...

  9. \Process(sqlservr)\% Processor Time 计数器飙高

    计数器" \Process(sqlservr)\% Processor Time",是经常监测,看看SQL Server如何消耗CPU资源.sqlserver是如何利用现有的资源; ...

  10. vmware一步步安装centos

    软件环境:vmware10.0破解版 centos版本:6.4 1.启动vmware,新建虚拟机,选择自定义安装 2,出现如下界面,保持默认,点击下一步 3.这个步骤要特别注意,选择“稍后安装”,我们 ...