这道题根据网上的一些代码和别人的讲解就是分为两种情况第一种:k*2<=len 直接比较, 第二种:就是会出现
有重叠部分,我们可以从结果入手, 最后一定是形成若干个长度为(len - k)的循环节(至于为什么网上看http://www.cnblogs.com/boring09/p/4373492.html)从每一个循环节中一个个比较,找出最大的字符出现数,最后输出的答案里加上其余的计数, 由于最大计数不计入ans,所以减去。
#include <iostream>
#include <cstdio>#include <cstring>
#include <map>
#include <algorithm>
#include <sstream>
#include <cctype>
#include <set>
#include <string>
#include <stack>
#include <vector>
using namespace std;
const int N = 1005;
const int inf = 0x3f3f3f3f;
int main()
{
int t, k, len, ans;
char str[N];
scanf("%d", &t);
while(t--)
{
scanf("%s %d", str, &k);
len = strlen(str);
ans = 0;
if(k * 2 <= len)
{
for(int i = 0, j = len - k ; j < len; i++ , j++)
ans += str[i]!=str[j]?1:0;
}
else
{
for(int i = 0; i < len - k ; i++)
{
int Count[4] = {0};
for(int j = i; j<len; j+=len-k)
{
if(str[j]=='A')
Count[0]++;
else if(str[j]=='T')
Count[1]++;
else if(str[j]=='C')
Count[2]++;
else if(str[j]=='G')
Count[3]++;
}
int Max = 0;
for(int j = 0; j<4;j++)
{
Max = max(Max, Count[j]);
ans+=Count[j];
}
ans -= Max;
}
}
printf("%d\n", ans);
}
return 0;
}