Maxim and Biology

版权声明:《本文为博主原创文章,转载请注明出处。 》 https://blog.csdn.net/zbq_tt5/article/details/89395750

传送门

Today in the scientific lyceum of the Kingdom of Kremland, there was a biology lesson. The topic of the lesson was the genomes. Let's call the genome the string "ACTG".

Maxim was very boring to sit in class, so the teacher came up with a task for him: on a given string ss consisting of uppercase letters and length of at least 44, you need to find the minimum number of operations that you need to apply, so that the genome appears in it as a substring. For one operation, you can replace any letter in the string ss with the next or previous in the alphabet. For example, for the letter "D" the previous one will be "C", and the next — "E". In this problem, we assume that for the letter "A", the previous one will be the letter "Z", and the next one will be "B", and for the letter "Z", the previous one is the letter "Y", and the next one is the letter "A".

Help Maxim solve the problem that the teacher gave him.

A string aa is a substring of a string bb if aa can be obtained from bb by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.

Input

The first line contains a single integer nn (4≤n≤504≤n≤50) — the length of the string ss.

The second line contains the string ss, consisting of exactly nn uppercase letters of the Latin alphabet.

Output

Output the minimum number of operations that need to be applied to the string ss so that the genome appears as a substring in it.

Examples

input

4
ZCTH

output

2

input

5
ZDATG

output

5

input

6
AFBAKC

output

16

Note

In the first example, you should replace the letter "Z" with "A" for one operation, the letter "H" — with the letter "G" for one operation. You will get the string "ACTG", in which the genome is present as a substring.

In the second example, we replace the letter "A" with "C" for two operations, the letter "D" — with the letter "A" for three operations. You will get the string "ZACTG", in which there is a genome.

题意:

给定一个字符串,输出字符串中连续的四位转换成“ACTG”的最小操作次数

题解:

用string进行操作,首先用string a,存储题目中给出的字符串,接着输入字符串,进行比较。

(其中,这里面有一个对C++里面输入流输出流的加速。)

注意:与a比较的时候,a的下标应该是j-i;

#include<iostream>
#include<string>
using namespace std;
string a="ACTG";
string num;
int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);//加速输入输出流
    int n,num_puts=3000,number;
    string num;
    cin>>n>>num;
    for(int i=0;i<n&&i+3<n;++i)
    {
        number=0;
        for(int j=i;j<i+4;++j)
        {
            number+=min(26-abs(a[j-i]-num[j]),abs(num[j]-a[j-i]));
        }
        num_puts=min(num_puts,number);
    }
    cout<<num_puts<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zbq_tt5/article/details/89395750