[TOJ 5065] the longest continuous subsequence and the longest toj5065
Description
Given a series of non-negative integers, find the longest continuous subsequence so that it is a multiple of 7.
Input
The first positive integer is N (1 <=n <= 50000), followed by N rows, each row has a non-negative integer, and All integers are not greater than 10 ^ 6.
Output
If a continuous subsequence exists and the subsequence is a multiple of 7, the length of the subsequence is output. If the subsequence does not exist, 0 is output.
Sample input
7
3
5
1
6
2
14
10
Sample output
5
#include<iostream> #include<cstring>#include<algorithm>using namespace std;int main(){ int n,i,j,a[50001],right[9],left[9],maxx=0,len; cin>>n; memset(right,-1,sizeof(right)); memset(left,-1,sizeof(left)); a[0]=0; left[0]=0; for(i=1;i<n;i++) { scanf("%d",&a[i]); a[i]=(a[i-1]+a[i])%7; if(left[a[i]]==-1) left[a[i]]=i; } for(i=n-1;i>=0;i--) { if(right[a[i]]==-1) right[a[i]]=i; } for(i=0;i<=8;i++) { for(j=0;j<=8;j++) { if(right[i]>=0&&left[j]>=0&&i==j) { len=right[i]-left[j]; maxx=max(len,maxx); } } } cout<<maxx<<endl; return 0;}