Original title link http://hihocoder.com/problemset/problem/1052
Problems: to set length of the composition of N ATCG string S, solving the least number of changes, so that the K character sequence after the first K character sequence identity, where 1 <= K <= N
analysis:
1) for the same length of the two strings A and B, the character string you want two become the same, all changes only where Ai = Bi, so that Ai = Bi or Bi = Ai can!;
2) When K <= N / 2, the first K character sequence with the K character sequence is no intersection, thus according to a) can be treated;
3) When K> N / 2, the first K character sequence with the character sequence of K intersection exists, as shown below:
For convenience of description, taking m = n - k, the same as the first K character sequence with the sequence of characters to meet the K, S the element must satisfy the relationship shown below is equal to:
These constraints equivalence relation corresponds to the set S is divided into the following characters U0 = {S0, Sm, ..., Sj * m}, U1 = {S1, Sm + 1, ..., Sj * m + 1 }, ..., Ui = {Si, Sm + i ... Sj * m + i}, where i <m and Sj * m + i <N. For each set Ui, just keep the same characters appear most frequently to replace other characters into the character can be, you need to modify the number of times = total number of characters - Maximum number of characters appear. Obviously, when K> N / 2, may also be so treated, but this time each set contains only two elements Ui.
Rules are as follows:
1) successively through each set U0 = {S0, Sm, ..., Sj * m}, U1 = {S1, Sm + 1, ..., Sj * m + 1}, ..., Ui = { si, Sm + i ... Sj * m + i}, where i <min (m, k) and Sj * m + i <N
2) For each set, you need to modify the number of times = total number of characters - the maximum number of characters appear
3) the accumulated number of modifications required to each set
code show as below:
1 def solve(): 2 T = int(input()) 3 for i in range(T): 4 S = raw_input() 5 K = int(raw_input()) 6 ret = do_solve(S.strip(" "), K) 7 print(str(ret)) 8 9 10 def do_solve(S, K): 11 ret = 0 12 step = len(S) - K 13 cnt = [0, 0, 0, 0] 14 for i in range(min(K, step)): 15 cnt[0] = cnt[1] = cnt[2] = cnt[3] = 0 16 j = i 17 while j < len(S): 18 if S[j] == 'A': 19 cnt[0] = cnt[0] + 1 20 elif S[j] == 'T': 21 cnt[1] = cnt[1] + 1 22 elif S[j] == 'C': 23 cnt[2] = cnt[2] + 1 24 elif S[j] == 'G': 25 cnt[3] = cnt[3] + 1 26 j += step 27 ret += sum(cnt) - max(cnt) 28 return ret 29 30 31 solve()