当前位置:网站首页>【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; }
边栏推荐
- The first part of the construction of the defense system of attack and defense exercise is the introduction and the four stages of Defense
- R language analyzing wine data
- 磁选机是什么?
- js打印99乘法表
- Construction of defense system for attack and defense exercises part II common strategies for responding to attacks
- 通过uview让tabbar根据权限显示相应数量的tabbar
- MySQL
- 语音信号特征提取流程:输入语音信号-分帧、预加重、加窗、FFT->STFT谱(包括幅度、相位)-对复数取平方值->幅度谱-Mel滤波->梅尔谱-取对数->对数梅尔谱-DCT->FBank->MFCC
- Gérer 1000 serveurs par personne? Cet outil d'automatisation o & M doit être maîtrisé
- Testing network connectivity with the blackbox exporter
猜你喜欢

js用switch输出成绩是否合格

无论LCD和OLED显示技术有多好,都无法替代这个古老的显示数码管

(resolved) NPM suddenly reports an error cannot find module 'd:\program files\nodejs\node_ modules\npm\bin\npm-cli. js‘

js中如何查看程序运行时间(计时器)

Stream常用操作以及原理探索

MySQL

win10-如何管理开机启动项?

游戏六边形地图的实现

MSSQL how to export and delete multi table data using statements

js输出形状
随机推荐
JDBC parameterized query example
JS print 99 multiplication table
JS performance reward and punishment examples
What is the difference between volatile and synchronized?
postgreSQL在windows系统遇到权限否认(permission denied)
延时队列`DelayQueue`
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%
JS output all prime numbers between 1-100 and calculate the total number
Installation and functions of uview
R 语言Analyzing wine data
专业四第二周自测
Common operation and Principle Exploration of stream
yarn create vite 报错 ‘D:\Program‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件
Sword finger offer 07 Rebuild binary tree
js打印99乘法表
Cookie encryption 7 fidder analysis phase
JS find the number of all daffodils
一個人管理1000臺服務器?這款自動化運維工具一定要掌握
University database mysql
R 中的 RNA-Seq 数据分析 - 调查数据中的差异表达基因!