The first question: The maximum number of sub-arrays and
Title Description
Given an array a[0,..., n-1], find its maximum subarray (length >=1) and
Enter a description
First line an integer n (1<=n<=5000), and then enter n integers (each integer range [-5000, 5000])
Output description
Outputs an integer representing the maximum number of sub-arrays and
Sample input
51-1 1 1-1
Sample output
2
#include <stdio.h>intMaxsum (int*a,intN) { intmax=-1; inti,j,sum; for(i=0; i<n;i++) {sum=0; for(j=i;j<n;j++) {sum+=A[j]; if(sum>max) Max=sum; } } returnMax;}intMainintargcConst Char*argv[]) { intLen; scanf ("%d",&Len); intI,a[len]; for(i=0; i<len;i++) scanf ("%d",&A[i]); printf ("%d", Maxsum (A,len)); return 0;}
Second question: The number of palindrome sequences of a string
Title Description
The number of palindrome sub-sequences (subsequence length >=1) for a string of not more than 15 length.
Enter a description
Enter a string that is not more than 15 in length, and the string is represented by a lowercase letter
Output description
Output the number of palindrome subsequence
Sample input
Abaa
Sample output
10
Comments
In this case, all of its palindrome subsequence are:
A,b,a,a,aba,aba,aa,aa,aa,aaa
A subsequence of a string is a new string that is stripped of some characters on the original string but does not break the relative position (in front or behind) of the remaining elements.
#include <string>#include<iostream>using namespacestd;stringSTR, creat="";intans=0;BOOLused[ -]={false};BOOLBack_forward (stringstr) { for(intI=0; I<str.length ()/2; i++) { if(Str[i]!=str[str.length ()-i-1]) return false; } return true;}voidSearchintLenintstart) { if(len<=0) { if(Back_forward (creat)) ans++; return; } for(inti = Start;i<str.length (); i++) { if(!Used[i]) {Used[i]=true; Creat.append (Str,i,1); Search (len-1, i+1); Used[i]=false; Creat.erase (Creat.length ()-1,1); } }}intMainintargcConst Char*argv[]) {cin>>str; for(intI=1; I<=str.length (); i++) Search (i,0); cout<<ans; return 0;}
Question three: array k decimal
Title Description
Given an array of integers a[0,..., n-1], find the K decimal in the array
Enter a description
First enter the array length n and K, where 1<=n<=5000, 1<=k<=n
Then output n shaping elements, the range of each number [1, 5000]
Output description
K Decimal in the array
Sample input
4 21 2) 3 4
Sample output
2
#include <vector>#include<algorithm>#include<iostream>using namespacestd;intMainintargcConst Char*argv[]) { intn,k; CIN>>n>>K; Vector<int>a (N,0); for(intI=0; i<n;i++) {cin>>A[i]; } sort (A.begin (), A.end ()); cout<<a[k-1]; return 0;}
Additional questions: Maximum sub-array and (enhanced version)
Title Description
Given an array a[0,..., n-1], find its maximum subarray (length >=1) and
Enter a description
First line an integer n (1<=n<=100000), and then enter n integers (each integer range [-5000, 5000])
Output description
Outputs an integer representing the maximum number of sub-arrays and
Sample input
51-1 1 1-1
Sample output
2
Comments
The subject and lab 6-1 are larger than the length of the array, and the efficiency of the program is more demanding.
#include <stdio.h>intMaxintXinty) { return(x>y)?x:y;}intMaxsum (int*a,intN) { inti; intNall,nstart; Nall=a[n-1]; Nstart=a[n-1]; for(i=n-2; i>=0; i--) {Nstart=max (a[i],a[i]+Nstart); Nall=Max (Nall,nstart); } returnNall;}intMainintargcConst Char*argv[]) { intLen; scanf ("%d",&Len); intI,a[len]; for(i=0; i<len;i++) scanf ("%d",&A[i]); printf ("%d", Maxsum (A,len)); return 0;}
Academy Online tsinghuax:00740043_2x C + + language programming advanced Sixth Lab