1. The longest continuous non-repeating subsequence
Given an integer sequence of length n, please find the longest continuous interval that does not contain repeated numbers, and output its length.
input format
The first line contains the integer n.
The second line contains n integers (all in the range 0∼105), representing a sequence of integers.
output format
A total of one line, containing an integer, indicating the length of the longest continuous interval that does not contain repeated numbers.
data range
1≤n≤105
Input sample:
5
1 2 2 3 5
Sample output:
3
code:
#include <iostream>
using namespace std;
const int N =1e6+10;
int n;
int a[N];
int s[N];//当前j-i区间内每一个数出现的次数
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;
}
2. The target and
Given two sorted arrays A and B sorted in ascending order, and a target value x.
Array subscripts start at 0.
Please find the number pair (i,j) that satisfies A[i]+B[j]=x.
The data is guaranteed to have a unique solution.
input format
The first line contains three integers n, m, x, respectively representing the length of A, the length of B and the target value x.
The second line contains n integers representing the array A.
The third line contains m integers representing array B.
output format
A row containing two integers i and j.
data range
Array length cannot exceed 105.
The elements in the same array are different.
1≤array elements≤109
Input sample:
4 5 6
1 2 4 7
3 4 6 8 9
Sample output:
1 1
code:
#include <iostream>
#include <algorithm>
using namespace std;
int m,n,x;
const int N=1e5+10;
int a[N],b[N];
int main()
{
scanf("%d%d%d",&n,&m,&x);
for(int i=0;i<n;i++) scanf("%d",&a[i]);
for(int i=0;i<m;i++) scanf("%d",&b[i]);
for(int i=0,j=m-1;i<n;i++)
{
while(j>=0 && a[i]+b[j]>x) j--;
if(a[i]+b[j]==x)
{
printf("%d %d\n",i,j);\
break;
}
}
return 0;
}
3. Judgment subsequence
Given a sequence of integers a1,a2,...,an of length nn and a sequence of integers b1,b2,...,bm of length m.
Please judge whether sequence a is a subsequence of sequence b.
A subsequence refers to a sequence in which some items of a sequence are arranged in the original order , for example, the sequence {a1,a3,a5} is a subsequence of the sequence {a1,a2,a3,a4,a5}.
input format
The first line contains two integers n,m.
The second line contains n integers denoting a1,a2,…,an.
The third line contains m integers representing b1,b2,…,bm.
output format
If sequence a is a subsequence of sequence b, output a row Yes
.
Otherwise, output No
.
data range
1≤n≤m≤105
−109≤ai,bi≤109
Input sample:
3 5
1 3 5
1 2 3 4 5
Sample output:
Yes
code:
#include <iostream>
#include <cstring>
using namespace std;
int n,m;
const int N=1e5+10;
int a[N];
int b[N];
int main()
{
cin>>n>>m;
for(int i=0;i<n;i++) cin>>a[i];
for(int i=0;i<m;i++) cin>>b[i];
int i=0,j=0;
while (i<n && j<m)
{
if(a[i]==b[j]) i++;
j++;
}
if(i==n) puts("Yes");
else puts("No");
return 0;
}