当前位置:网站首页>【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; }
边栏推荐
- Apifox learning
- Speech signal processing - concept (II): amplitude spectrum (STFT spectrum), Mel spectrum [the deep learning of speech mainly uses amplitude spectrum and Mel spectrum] [extracted with librosa or torch
- Guava scheduled task
- (已解决) npm突然报错 Cannot find module ‘D:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js‘
- 使用 Blackbox Exporter 测试网络连通性
- Yarn create vite reports an error 'd:\program' which is neither an internal or external command nor a runnable program or batch file
- js输出1-100之间所有的质数并求总个数
- js来打印1-100间的质数并求总个数优化版
- 什么是期货反向跟单?
- 2、项目使用的QT组件
猜你喜欢

JS to determine whether the result is qualified, the range is 0-100, otherwise re-enter

js中判断成绩是否合格,范围在0-100,否则重新输入

Online text digit recognition list summation tool

Error in idea connection database

VNC Viewer方式的远程连接树莓派

使用 Blackbox Exporter 测试网络连通性

One person manages 1000 servers? This automatic operation and maintenance tool must be mastered

二叉树结构以及堆结构基础

How to view program running time (timer) in JS

MySQL
随机推荐
JS find the number of all daffodils
通过uview让tabbar根据权限显示相应数量的tabbar
闭包问题
js成绩奖惩例题
Hutool symmetric encryption
File and multipartfile overview
js用switch语句根据1-7输出对应英文星期几
The first part of the construction of the defense system of attack and defense exercise is the introduction and the four stages of Defense
正斜杠反斜杠的由来
Coal crusher
移动安全工具-jad
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%
ACM课程学期总结
cookie加密7 fidder分析阶段
JS print 99 multiplication table
win命令行中导入、导出数据库相关表
File 与 MultipartFile概述
What are the specialties of database system engineers?
RNA SEQ data analysis in R - investigate differentially expressed genes in the data!
Jupiter notebook file directory