python代码(2)---google中国编程挑战赛入围赛真题HardDuplicateRemover(1000分)

问题分析(问题描述见后面):

首先建立一个dup表,来记录duplicate item下一次出现的下标,如果没有下一相同的item,则记为0.

循环dup表:

       当前位置i

       如果没有duplicate值,直接写到输出tlist中.

      有duplicate值:

          寻找 head , tail下标值. head=i(当前循环到的位置), tail=min(head后的每一个没有duplicate 的item的下标,最后duplicate项的下标)

          在 head---tail 这段区域中判断是否有比当前item更小的值:

                是----舍弃当前值

                否----输出到tlist中,将duplicate值标记为已经舍弃.


代码:

def  findtail(dup,i):
    
"""  find the tail of dupicate items' index """
    
if  dup[i] > 0:
        
return  findtail(dup,dup[i])
    
else :
        
return  i
    
pass
def  markRemove(dup,i):
    
"""  i for reserved,then others mark remove(-1) """
    k
= dup[i]
    dup[i]
=- 1
    
if  k > 0:
        markRemove(dup,k)
    
pass
def  tmin(list,dup):
    
"""  find the min in list,exclude the mark remove items in the dup """
    m
= 65535
    
for  i  in  range(len(dup)):
        
if  dup[i] >= and  m > list[i]:
            m
= list[i]
    
return  m
    
pass
def  hdr(list):
    
# First create the whether duplicate list,0 for single,sub for multi
    dup = [0] * len(list)
    iterate
= range(len(list))
    
for  i  in  iterate:
        
if  list[i]  in  list[i + 1 :]:
            dup[i]
= list[i + 1 :].index(list[i]) + i + 1
            
pass
        
pass
    
# finally Remove the duplicate items
    tlist = []
    
for  i  in  iterate:
        
if  dup[i] > 0:
            tail
= findtail(dup,i)
            
if  0  in  dup[i + 1 :tail]:
                tail
= dup[i + 1 :].index(0) + i + 1
            
if  list[i] <= tmin(list[i + 1 :tail + 1 ],dup[i + 1 :tail + 1 ]):
                tlist.append(list[i])
                markRemove(dup,i)                          
            
pass
        
elif  dup[i] == 0:
            tlist.append(list[i])
    
return  tlist

if   __name__ == " __main__ " :
    list
= [ 1 , 2 , 1 , 4555 , 9 , 2 , 78 , 1 , 90 , 111 ]
    
print  list
    
print   " === " ,hdr(list)
    list
= [ 5 , 4 , 6 , 5 , 45 , 4 , 9 , 6 , 78 , 1 , 9 , 45 , 6 , 29 ]
    
print  list
    
print   " === " ,hdr(list)    

 

问题描述:
We have a sequence of integers, and we would like to remove all duplicate elements from this sequence. There may be multiple ways to perform this task. For example, given the sequence { 1, 2, 1, 3 }, we could end up with either { 1, 2, 3 } or { 2, 1, 3 } as the remaining sequence, depending on which duplicate 1 we remove from the original sequence. For this problem, we want to return the lexicographically first of of all possible remaining sequences. A sequence S1 comes before sequence S2 lexicographically if and only if S1 has a smaller value than S2 at the lowest index at which the two sequences differ (so, for example, { 1, 2, 3 } comes before { 2, 3, 1 }).
You will be given a int[] sequence. Return a int[] containing the sequence after all the duplicates are removed. See the examples for further clarification.
Definition
Class:
HardDuplicateRemover
Method:
process
Parameters:
int[]
Returns:
int[]
Method signature:
int[] process(int[] sequence)
(be sure your method is public)
Problem Statement

Constraints
-
sequence will have between 1 and 50 elements, inclusive.
-
Each element of sequence will be between 1 and 1000, inclusive.
Examples
0)

{5, 6, 5, 1, 6, 5}
Returns: {1, 6, 5 }
There are six different ways to remove duplicates (remaining numbers are marked by '*'):
{ *5, *6,  5, *1,  6,  5},
{ *5,  6,  5, *1, *6,  5},
{  5, *6, *5, *1,  6,  5},
{  5,  6, *5, *1, *6,  5},
{  5, *6,  5, *1,  6, *5},
{  5,  6,  5, *1, *6, *5}.

The last variant is the lexicographically first.
1)

{3, 2, 4, 2, 4, 4}
Returns: {3, 2, 4 }

2)

{6, 6, 6, 6, 6, 6}
Returns: {6 }

3)

{1, 3, 2, 4, 2, 3}
Returns: {1, 2, 4, 3 }

4)

{5, 4, 1, 5}
Returns: {4, 1, 5 }

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

声明:所有代码皆为作者原创,欢迎回复讨论.转载或引用请注明出处.
题外语:对这个问题,很多人都给出了自己的算法,其中定有许多精妙处,可惜作者没有说明他的思路,百看不得其真意.我想贴代码无非是为了知识的共享,你要让别人很容易明白你在说什么,那么最好写一下伪代码或说明一下思路吧,毕竟没有人真正的会拷贝下你的代码一点点调试.
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值