C. DNA Alignment(Codeforces Round #295(div2))

C. DNA Alignment
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Sample test(s)
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



题意:

      两个字符串,p值为将将两个字符串最右面的字符移到最左边的所有可能的h值的和,当两个字符串有相同的,取1,否则取0,现给出一个字符串,求所有p值取最大值的另一个字符串。

题解:

       首先要明白,由于是可以移动位置的,所以要想使p值最大,则另一个字符串只可能出现出现次数最多的字符。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=100000+1000;
const int mod=1000000000+7;
char s[maxn];
int n;
int a[5];
long long ans;
long long pow_q(long long x,int n)
{
    ans=1;
    while(n)
    {
        if(n&1)
        ans=(ans*x)%mod;
        x=(x*x)%mod;
        n=n>>1;
    }
    return ans;
}
int main()
{
    while(~scanf("%d",&n))
    {
        scanf("%s",s);
        memset(a,0,sizeof(a));
        for(int i=0;i<n;i++)
        {
            if(s[i]=='A')
            a[0]++;
            if(s[i]=='C')
            a[1]++;
            if(s[i]=='G')
            a[2]++;
            if(s[i]=='T')
            a[3]++;
        }
        sort(a,a+4);
        int maxc=a[3];
        int cou=0;
        for(int i=0;i<4;i++)
        if(a[i]==maxc)
        cou++;
        long long ans;
        ans=pow_q(cou,n);
        cout<<ans<<endl;
    }
    return 0;
}


你可能感兴趣的:(Algorithm,编程,ACM,codeforces)