Perfect World Recruitment written test (sub series) _ Perfect World

Source: Internet
Author: User
Topic Description:
Given an array of length n, find one of the longest monotone self-increment sequences (not necessarily sequential, but not chaotic)
For example: Given a length of 8 array a{1,3,5,2,4,6,7,8},
The longest monotone increment sequence is {1,2,4,6,7,8}, with a length of 6.
Enter a description:
The first line contains an integer t that represents the number of test data sets.
For each set of test data: N-The length of the array
A1 A2. An (array to compute)
Guarantee: 1<=n<=3000,0<=ai<=max_int.
Output Description:
For each set of data, output an integer that represents the length of the longest ascending subsequence.
Enter an example:
2
7
89 256 78 1 46 78 8
5
6 4 8 2 17
Output Example:
3
3
Analysis: This problem to tell the truth also bothered me for two days, just want to understand this evening, the actual implementation is very simple, that is, loop inline loop This comparison means inside. A lot of people talk about dynamic planning, I'm sorry, my ability is limited, for dynamic planning, I think is to set a temporary variable to decide to continue not to continue a few procedures. I have two parts for this problem:
The first part, which is called the longest ascending subsequence of a unordered sequence to find it. The implementation is through the three-tier loop traversal, get the final desired results, parse and implement the code as follows:
#-*-coding:utf-8-*-def calmaxlen (objlist): b = [] #b是用来存放每一次遍历或者说选择好的递增序列的长度的 for I in range (len (objlist)): #print ' When I =%d: '% i #max = objlist[i] for j in Range (I+1,len (objlist)): print ' When i= %d,from%dth ele: '% (i,j) #当第i次循环, which means that the element labeled I is the #递增序列的第一个 we're looking for, and at this point we start #元素开始 from behind I traversed, k = J #请注意k是什么呢. is a marker, when the I cycle #也就是说下标为i的元素为递增序列第一个元素时 #我们依次要选择从i + 1, i+2, I+3...i+len (objlist) Start #因为递增序列中的
            Elements may not be contiguous in the original sequence, but must satisfy the #递增. A = [] #a用来存放每一次最基本的循环后的递增序列 the increment sequence max = Objlist[i] that we found when #比如i =0,j=1 #我们每次把开
                The start element is set to maximum, so select A.append (Objlist[i]) #先把起始元素加进递增表里 for M in range (K,len (objlist)):
                    If objlist[m] >= Max:max = objlist[m] A.append (objlist[m)) #如果从下标为j的元素后算起.
  Update Max else if greater than Max:                  Continue # Less then keep looking, continue print ' The addly list is ', a # Output the increment sequence b.append (len (a)) #然后把这个递增序列的长度放到最终各个循环次数下的 # increment sequence length table if i = = L
            En (objlist) -1:a = [] A.append (Objlist[i]) print ' When I was the last ele: ' print ' The addly list is only: ', a #这里要注意, when I equals the subscript of the last element #上面的j就取不到了, this time we'll print the last element Because it must be a cell element c = b[0] for i in range (len (b)): If C <= b[i]: c = b[i] return c s = map (i
 Nt,raw_input (). Split ()) d = Calmaxlen (s) print ' So, the Max length of the addly list in the objective list is: ', D

E:\Python27\python.exe D:/python/python Algorithm/b.py
1 5 2 4
When i= 0,from 1th ele:
The addly list is [1, 5]
When i= 0,from 2th ele:
The addly list is [1, 2, 4]
When i= 0,from 3th ele:
The addly list is [1, 4]
When i= 1,from 2th ele:
The addly list is [5]
When i= 1,from 3th ele:
The addly list is [5]
When i= 2,from 3th ele:
The addly list is [2, 4]
When I was the last ele:
The addly list is only: [4]
So, the Max length of the addly list in the objective list Is:3

Process finished with exit code 0
That's the problem. The solution to the core process
The second part is the output for the purpose of this problem, and of course I will comment out the unnecessary output:
#-*-coding:utf-8-*-def calmaxlen (objlist): b = [] #b是用来存放每一次遍历或者说选择好的递增序列的长度的 for I in range (len (objlist)):  #print ' When I =%d: '% i #max = objlist[i] for j in Range (I+1,len (objlist)): #print ' when i= %d,from%dth ele: '% (i,j) #当第i次循环, which means that the element labeled I is the #递增序列的第一个 we're looking for, and at this point we start #元素开 from behind I began to traverse, k = J #请注意k是什么呢. is a marker, when the I cycle #也就是说下标为i的元素为递增序列第一个元素时 #我们依次要选择从i + 1, i+2, I+3...i+len (objlist) Start #因为递增序列中的
            Elements may not be contiguous in the original sequence, but must satisfy the #递增. A = [] #a用来存放每一次最基本的循环后的递增序列 the increment sequence max = Objlist[i] that we found when #比如i =0,j=1 #我们每次把开
                The start element is set to maximum, so select A.append (Objlist[i]) #先把起始元素加进递增表里 for M in range (K,len (objlist)):
                    If objlist[m] >= Max:max = objlist[m] A.append (objlist[m)) #如果从下标为j的元素后算起.
 Update Max else if greater than Max:                   Continue # Less then keep looking, continue #print ' The addly list is ', a  #输出此次循环后得到的递增序列 B.append (Len (a)) #然后把这个递增序列的长度放到最终各个循环次数下的 # increment sequence length table if i = =
            Len (objlist) -1:a = [] A.append (Objlist[i]) #print ' When I was the last ele: '
            #print ' The addly list is only: ', a #这里要注意, when I equals the subscript of the last element #上面的j就取不到了, this time we'll print the last element #因为它必然就是一个单元素 C = b[0] for i in range (len (b)): If C <= b[i]: c = b[i] return c = i
    NT (Raw_input ()) #n是输入的组数 endlist = [] for i in range (n): R = Int (Raw_input ()) s = map (int, raw_input (). Split ()) Endlist.append (Calmaxlen (s)) for I in Endlist:print I
Output Example:
E:\Python27\python.exe D:/python/python Algorithm/maxaddlylist.py
2
7
89 256 78 1 46 78 8
5
6 4 8 2 17
3
3

Process finished with exit code 0
ok! to call it a wrap.
Summed up is: the idea to clear, as to say whether the program is still how to optimize, I hope the great God to guide.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.