题目描述:

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

输入:

输入包含多个测试用例,每组测试用例输入一行由小写英文字符a,b,c...x,y,z组成的字符串,字符串的长度不大于10000。

输出:

对于每组测试用例,输出最大长度的不重复子串长度。

样例输入:
absd
abba
abdffd
样例输出:
4
2
4

阿尔卡特2013年实习生招聘笔试题

 #include <iostream>
using namespace std; int main() {
string s;
while (cin >> s) {
int *a = new int[s.length()];
int max = ;
bool flag;
a[] = ;
for (int i = ; i < s.length(); ++i) {
flag = true;
for (int j = ; j <= a[i - ]; ++j) {
if (s[i - j] == s[i]) {
a[i] = j;
flag = false;
break;
}
}
a[i] = flag ? a[i - ] + : a[i];
max = max > a[i] ? max : a[i];
}
cout << max << endl;
}
return ;
}

滑动窗口!

 #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std; string s;
vector<int> pos; void solve() {
int start = , len = ;
pos.assign(, -);
for (int i = ; i < s.length(); ++i) {
if (pos[s[i]] >= start) {
start = pos[s[i]] + ;
} else {
len = max(len, i - start + );
}
pos[s[i]] = i;
}
cout << len << endl;
} int main() {
while (cin >> s) {
solve();
}
return ;
}
/**************************************************************
Problem: 1530
User: hupo250
Language: C++
Result: Accepted
Time:50 ms
Memory:1520 kb
****************************************************************/

[Jobdu] 题目1530:最长不重复子串的更多相关文章

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

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

  2. 九度oj 题目1530:最长不重复子串

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

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

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

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

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

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

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

  6. hdu_2668 Daydream O(n)求最长不重复子串

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2668 Daydream Time Limit: 2000/1000 MS (Java/Others)  ...

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

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

  8. 最长无重复子串问题 leetcode 3

    一.代码及注释 class Solution { public: int lengthOfLongestSubstring(string s) { int n = s.size(); //字符串的长度 ...

  9. LeetCode03 最长无重复子串

    题目 给定一个字符串,找出不含有重复字符的最长子串的长度. 解答 刚开始以为只是一遍遍历后来的字符和前面一样便开始算新子串,给的案例都过了,但是卡在了"dvdf" 后来经过重重试验 ...

随机推荐

  1. ML—朴素贝叶斯

    华电北风吹 日期:2015/12/12 朴素贝叶斯算法和高斯判别分析一样同属于生成模型.但朴素贝叶斯算法须要特征条件独立性如果,即样本各个特征之间相互独立. 一.朴素贝叶斯模型 朴素贝叶斯算法通过训练 ...

  2. python多线程概念

    转自:http://www.cnblogs.com/fnng/p/3489321.html 在使用多线程之前,我们首页要理解什么是进程和线程. 什么是进程? 计算机程序只不过是磁盘中可执行的,二进制( ...

  3. ArcGIS Pro体验04——菜单栏

    对菜单栏进行熟悉一下: 1.地图菜单 剪切板(Clipboard):剪切(Cut).复制(Copy).粘贴(Paste),这些不用说了,在ArcMap中是放在"编辑"菜单下面的.当 ...

  4. 栈的实现实例(C语言)

    /* stack.h */ #ifndef _stack_h #define _stack_h struct stack_record; typedef struct stack_record *st ...

  5. CentOS 下安装MySQL 默认源为5.1版本

    CentOS——默认为安装5.1版本,如果需要安装5.5版本,需要使用remi源 yum install mysql-server –enablerepo=remi   Ubuntu——默认为安装5. ...

  6. js replace 如何替换字符串中的最后一个匹配项

    1.正则表达时,贪婪模式,.*会一直匹配到最后一个 // 验证 let str = "123[空]345[空]789[空]0"; let res = str.replace(/(. ...

  7. 安装ubuntu后不能从ubuntu引导修复方法

    sudo fdisk -l sudo -i mkdir /media/tempdir mount /dev/sda7 /media/tempdir grub-install --root-direct ...

  8. 〖Linux〗apt-get wait for another apt process

    #!/bin/bash i= tput sc >& || \ >&; do )) in ) j="-" ;; ) j="\\" ;; ...

  9. python之poplib模块下载并解析邮件

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之poplib模块下载并解析邮件 #https://github.com/michaelliao ...

  10. HTTP协议详解之响应篇

    #xiaodeng #状态码 #HTTP权威指南 62 #http响应由3部分组成:状态行.消息报头.响应正文.HTTP-Version Status-Code Reason-Phrase CRLF# ...