关键还是要把问题想通透,然后就很简单了。
'''
CodeJam Practice 2009 qualification round question C
Created on 2012-12-18
@author: festony
'''
from cj_lib import *
from properties import *
curr_file_name = 'C-large-practice'
#curr_file_name = 'C-small-practice'
#curr_file_name = 'test'
def input_dividing_func(input_lines):
total_case = int(input_lines.pop(0))
case_inputs = input_lines[:total_case]
return case_inputs
target = 'welcome to code jam'
def count_time_char(c, time_counts_char):
for i, t in enumerate(target):
if t == c:
time_counts_char[i] += time_counts_char[i+1]
time_counts_char[i] = time_counts_char[i] % 10000
# time_counts: holds how many times has a certain char sequence appeared in previous str.
# each element of time_counts is also a list:
# 0: time counts for 'welcome to code jam'
# 1: time counts for 'elcome to code jam'
# 2: time counts for 'lcome to code jam'
# 3: time counts for 'come to code jam'
# 4: time counts for 'ome to code jam'
# 5: time counts for 'me to code jam'
# 6: time counts for 'e to code jam'
# 7: time counts for ' to code jam'
# 8: time counts for 'to code jam'
# 9: time counts for 'o code jam'
# 10: time counts for ' code jam'
# 11: time counts for 'code jam'
# 12: time counts for 'ode jam'
# 13: time counts for 'de jam'
# 14: time counts for 'e jam'
# 15: time counts for ' jam'
# 16: time counts for 'jam'
# 17: time counts for 'am'
# 18: time counts for 'm'
# 19: 1 to simplify calculation.
#
# theory: Check input string from end to beginning. At any point, say, for example if
# 'w' appears one time, and after this 'w', seq 'elcome to code jam' has appearred 5 times,
# then full term 'welcome to code jam' till now have 5 different position combination (
# time_counts_char[0] was 0, time_counts_char[1] was 5, now time_counts_char[0] +=
# time_counts_char[1], then time_counts_char[0] becomes 5). Then keep going forward to the
# beginning of the input string, suddenly 'w' appears again, then combine with previous one
# 'w' and 5 different 'elcome to code jam', full term now may appear 10 times(
# time_counts_char[0] was 5, time_counts_char[1] was 5, now time_counts_char[0] +=
# time_counts_char[1], then time_counts_char[0] becomes 10). etc etc.
def process_func(func_input):
in_seq = list(func_input)
in_seq.reverse()
time_counts = [[0]*len(target) + [1]]*len(in_seq)
for i, time_counts_char in enumerate(time_counts):
count_time_char(in_seq[i], time_counts_char)
return '%04d' % time_counts[len(in_seq) - 1][0]
run_proc(process_func, input_dividing_func, curr_working_folder, curr_file_name)