[Codeforces Round #295]DNA Alignment

Description

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,
[Codeforces Round #295]DNA Alignment_第1张图片

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

My Problem Report

这道题的核心是解决两个问题,第一是如何求p函数和shift函数,第二是如何求p值最大的字符串的数目。
第一个问题

很容易可以看出,长度为n的字符串,需要shift n^2次。
shift操作可进一步简化

假设字符串分别为A和B,我们按A串的字符顺序(循环),依次往它尾部添加字符,直到其长度为n+n^2-1,我们把新串叫做A’
保持延长后的A’串不动,B串串头与A’串串头对齐,并一位位向后移动,一共移动n+n^2-1次,加上初始状态,一共匹配了n^2次。如图所示:

AGAGA
AG
AGAGA
~AG
AGAGA
~~AG
AGAGA
~~~AG

第二个问题

在构造B串的过程中,我们发现,我们所选取的每一个字母,在都是在A串中出现次数最多的字母,例如有A串

AGCTT

其中T出现了两次,我们只能构造这样的一个B串

TTTTT

若出现次数最多的字母不唯一,那么相当于B串每一位我们都有不唯一的选择,设我们有x种选择,那么最后构造出的B串种数就是x^n,因此我们就可以得到最终答案:x^n mod 10e9+7

My Code

//  Created by Chlerry in 2015.
//  Copyright (c) 2015 Chlerry. All rights reserved.
//  http://codeforces.com/contest/520/problem/C

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define ll long long
const ll M=1000000007;
int n;
string input;
map<char,ll> a;
const string s="ATCG";
ll maxn=-1,cnt,ans=1;

int main()
{
    //freopen("in.txt","r",stdin);
    cin>>n>>input;
    for(int i=0;ifor(int i=0;i<4;i++)
        if(a[s[i]]==maxn)
            cnt++;
        else if(a[s[i]]>maxn)
        {
            maxn=a[s[i]];
            cnt=1;
        }
    for(int i=0;icout<return 0;
}

你可能感兴趣的:(------------,Math,------------,String,Codeforces)