当前位置:网站首页>【12. 最大连续不重复子序列】
【12. 最大连续不重复子序列】
2022-06-27 07:42:00 【小呆鸟_coding】
双指针算法的俩种情况

#include <iostream>
#include <string.h>
#include <cstdio>
using namespace std;
int main()
{
char str [1000];
//scanf("%s", str);
fgets(str,sizeof(str), stdin);
int n = strlen(str);
for (int i = 0; i < n; i ++)
{
int j = i;
while (j < n && str[j] != ' ') j ++;
// 这道题的具体逻辑
for (int k = i; k < j; k ++) cout << str[k];
cout << endl;
i = j;
}
return 0;
}
运行结果:
输入:
abc def cde
输出:
abc
def
cde
最大连续不重复子序列

- 额外开辟一个数组S[N],动态的记录一下,元素出现了多少次,相当于i每次往后移动一格,就往S[N]中加入一个元素,如果j往前移动一格,相当于出现重复数字,在从S[N]中删除掉该元素。
- 最后可以动态的统计出,该数组中有多少个数
给定一个长度为 n 的整数序列,请找出最长的不包含重复的数的连续区间,输出它的长度。
题目
给定一个长度为 n 的整数序列,请找出最长的不包含重复的数的连续区间,输出它的长度。
输入格式
第一行包含整数 n。
第二行包含 n个整数(均在 0∼100000范围内),表示整数序列。
输出格式
共一行,包含一个整数,表示最长的不包含重复的数的连续区间的长度。
数据范围
1 ≤ n ≤ 100000
输入样例:
5 1 2 2 3 5输出样例:
3
代码
#include<iostream> using namespace std; const int N = 100010; int n; int a[N],s[N]; int main() { cin >> n; for (int i = 0; i < n; i ++) cin >> a[i]; int res = 0; for (int i = 0, j = 0; i < n; i ++) { s[a[i]] ++; while (s[a[i]] > 1) { s[a[j]] --; j ++; } res = max(res, i - j + 1); } cout << res << endl; return 0; }
边栏推荐
- js求所有水仙花数
- 语音合成:Tacotron详解【端到端语音合成模型】【与传统语音合成相比,它没有复杂的语音学和声学特征模块,而是仅用<文本序列,语音声谱>配对数据集对神经网络进行训练,因此简化了很多流程】
- (笔记)Anaconda-Navigator闪退解决方法
- 游戏六边形地图的实现
- Implementation principle of similarity method in Oracle
- 2022爱分析· IT运维厂商全景报告
- How to view program running time (timer) in JS
- Apifox learning
- What is a magnetic separator?
- JS to determine whether the number entered by the user is a prime number (multiple methods)
猜你喜欢
![log4j:WARN No such property [zipPermission] in org.apache.log4j.RollingFileAppender.](/img/2c/425993cef31dd4c786f9cc5ff081ef.png)
log4j:WARN No such property [zipPermission] in org.apache.log4j.RollingFileAppender.

索引+sql练习优化

JS to determine whether the number entered by the user is a prime number (multiple methods)

win命令行中导入、导出数据库相关表

MySQL
![[Software Engineering] software engineering review outline of Shandong University](/img/38/2c783df56b50dee3bbb908f6f3e70e.png)
[Software Engineering] software engineering review outline of Shandong University

2. QT components used in the project

JS uses the while cycle to calculate how many years it will take to grow from 1000 yuan to 5000 yuan if the interest rate for many years of investment is 5%
![【批处理DOS-CMD命令-汇总和小结】-批处理命令中的参数%0、%1、%2、%[0-9]、%0-9和批处理命令参数位置切换命令shift,dos命令中操作符%用法](/img/05/19299c47d54d4ede95322b5a923093.png)
【批处理DOS-CMD命令-汇总和小结】-批处理命令中的参数%0、%1、%2、%[0-9]、%0-9和批处理命令参数位置切换命令shift,dos命令中操作符%用法

js判断用户输入的数是否为质数(多种方法)
随机推荐
js中输入三个值,并且由小到大输出
期货反向跟单靠谱吗?
R 语言 基于关联规则与聚类分析的消费行为统计
Speech signal processing - concept (I): time spectrum (horizontal axis: time; vertical axis: amplitude), spectrum (horizontal axis: frequency; vertical axis: amplitude) -- Fourier transform -- > time
File 与 MultipartFile概述
The first part of the construction of the defense system of attack and defense exercise is the introduction and the four stages of Defense
语音合成:Tacotron详解【端到端语音合成模型】【与传统语音合成相比,它没有复杂的语音学和声学特征模块,而是仅用<文本序列,语音声谱>配对数据集对神经网络进行训练,因此简化了很多流程】
基础知识 | js基础
R 中的 RNA-Seq 数据分析 - 调查数据中的差异表达基因!
Guava tutorial collect some cases and write Google tool classes slowly
js中如何查看程序运行时间(计时器)
Use uview to enable tabbar to display the corresponding number of tabbars according to permissions
js中判断成绩是否合格,范围在0-100,否则重新输入
闭包问题
Idea method template
Cookie encryption 7 fidder analysis phase
(笔记)Anaconda-Navigator闪退解决方法
js输出1-100之间所有的质数并求总个数
Set the address book function to database maintenance, and add user name and password
js判断用户输入的数是否为质数(多种方法)