DNA Alignment CodeForces - 520C(思维)

Vasya became interested in bioinformatics. He’s going to write an article about similar cyclic DNA sequences, so he invented a new method for determining the similarity of cyclic sequences.

Let’s assume that strings s and t have the same length n, then the function h(s, t) is defined as the number of positions in which the respective symbols of s and t are the same. Function h(s, t) can be used to define the function of Vasya distance ρ(s, t):
where is obtained from string s, by applying left circular shift i times. For example,
ρ(“AGC”, “CGT”) = 
h(“AGC”, “CGT”) + h(“AGC”, “GTC”) + h(“AGC”, “TCG”) + 
h(“GCA”, “CGT”) + h(“GCA”, “GTC”) + h(“GCA”, “TCG”) + 
h(“CAG”, “CGT”) + h(“CAG”, “GTC”) + h(“CAG”, “TCG”) = 
1 + 1 + 0 + 0 + 1 + 1 + 1 + 0 + 1 = 6

Vasya found a string s of length n on the Internet. Now he wants to count how many strings t there are such that the Vasya distance from the string s attains maximum possible value. Formally speaking, t must satisfy the equation: .

Vasya could not try all possible strings to find an answer, so he needs your help. As the answer may be very large, count the number of such strings modulo 109 + 7.
Input

The first line of the input contains a single integer n (1 ≤ n ≤ 105).

The second line of the input contains a single string of length n, consisting of characters “ACGT”.
Output

Print a single number — the answer modulo 109 + 7.
Examples
Input

1
C

Output

1

Input

2
AG

Output

4

Input

3
TTT

Output

1

Note

Please note that if for two distinct strings t1 and t2 values ρ(s, t1) и ρ(s, t2) are maximum among all possible t, then both strings must be taken into account in the answer even if one of them can be obtained by a circular shift of another one.
In the first sample, there is ρ(“C”, “C”) = 1, for the remaining strings t of length 1 the value of ρ(s, t) is 0.

In the second sample, ρ(“AG”, “AG”) = ρ(“AG”, “GA”) = ρ(“AG”, “AA”) = ρ(“AG”, “GG”) = 4.

In the third sample, ρ(“TTT”, “TTT”) = 27
题目链接
参考题解1
参考题解2
给定长度为n的一个字符串s。
构造长度也为n的字符串t。使得p(s,t)值最大,问有多少个不同的t,emmm,这个题目看了题解也是没看懂为什么这么做。大体意思好像是,找到里面出现次数最多的,那几个字母。因为,你每确定一个字母,那么排列组合的时候,就会出现在n个位置上,这样就提供了n的价值,找到出现次数最多的,那么提供的价值就最大。找出s中出现最多的次数。如果这个次数对应的字母k个,答案就是kn(每个位置可以有k种选择,n个位置)

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAX_N = 100007;
const long long mod = 1e9 + 7;
char str[MAX_N];
int a[5], n;

long long Pow(long long x, long long n)
{
    long long res = 1;
    while (n > 0)
    {
        if (n & 1) res = res * x % mod;
        x = x * x % mod;
        n >>= 1;
    }
    return res;
}

int main()
{
    scanf("%d%s", &n, str);
    for (int i = 0; str[i]; ++i)
    {
        if (str[i] == 'A') ++a[0];
        if (str[i] == 'C') ++a[1];
        if (str[i] == 'G') ++a[2];
        if (str[i] == 'T') ++a[3];
    }
    int up = 0;
    for (int i = 0; i < 4; ++i) up = max(up, a[i]);
    int cnt = 0;
    for (int i = 0; i < 4; ++i) if (a[i] == up) ++cnt;
    long long ans = Pow(cnt, n);
    printf("%lld\n", ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_40788897/article/details/85259237
DNA