Description
2015 Guangdong University of Technology ACM School Race to come to ~\ (≧▽≦)/~ spicy spicy, as one of the school game, GG came up with a water problem to test everyone. Believe that the small partners have learned the string match, so the string matching water problem is born spicy! GG gives a sequence of uppercase letters of length n, and now he wants you to modify this sequence of letters so that the first K-letter sequence on this sequence of letters matches the K-letter sequence one by one of the last face.
For example, for the sequence "Atuuuuac" and k = 2, you can change the second letter to "C", so that the first two letters and the last two letters are "AC", of course, there are other methods of modification, now GG asks you to find out how many letters to make a string match at least.
Input
There is a T set of data input. (T <= 100)
Each set of data has only two rows, the first behaves as a string, the second behaves as a positive integer k, and the string length does not exceed 1000 and is at least 1. (1 <= K <= N).
Output
Minimum number of letters to modify for each set of data output
Sample Input2Atuuuuac2ATACGTCT6Sample Output13
Test instructions: This problem may be some people can not read test instructions, modify the characters may affect the characters of the two substrings, so the second sample data, although the two substrings have 4 characters do not match, but only need to change 3 times.
Analysis: hypothesis n=8,k=6;
In the diagram, the lines are connected together to be identical.
We find that every n-k character is the same, then we only need for each I, subscript i + M * (n-k) The most occurrences of the letter is ' * ', then we will put i + M * (n-k) These characters are all changed to ' * ';
This is a greedy thought.
On the code:
#include <stdio.h> #include <string.h>char str[10000];int n,n,j,k,m,a[26];int Find (int x) { int c=0; memset (A,0,sizeof (a)); while (X<n) { a[str[x]-' a ']++; X=x+m; C + +; } int max=0; for (int i=0;i<26;i++) max= (Max>a[i])? Max:a[i]; return C-max;} int main () { scanf ("%d", &n); while (n--) { scanf ("%s", str); scanf ("%d", &k); N=strlen (str); M=n-k; int ans=0; for (int i=0;i< k && i< m;i++) { ans+=find (i); } printf ("%d\n", ans); } return 0;}
GG Match String ___ (Guangdong University of Technology 2015 School match Preliminary)