1. 程式人生 > >【打CF,學演算法——二星級】CodeForces 520C DNA Alignment (構造)

【打CF,學演算法——二星級】CodeForces 520C DNA Alignment (構造)

題面:

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 functionh(s, t) is defined as the number of positions in which the respective symbols ofs and t arethe same. Function h(s, t) can be used to define the function of Vasya distanceρ(s, t):

where is obtained from strings, 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 modulo109 + 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 andt2 values ρ(s, t1) и ρ(s, t2) are maximum among all possiblet, 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 stringst 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

題意:

     給定一個n長度的由ACGT四種序列構成的DNA序列。問有多少種方案,構造另一種序列,使得兩種序列在shift過程中,兩者相同的位數總和最大。

     shift操作即移位操作,兩者分別移動,然後求相同位的個數。

解題:

      首先可以統計出ACGT每種序列的個數,題意是讓我們構造一個ACGT序列,假如某一元素數量最大為k,明顯是將所有的數量都分配給該元素,這樣求得的值最大。但可能存在幾種元素數量並列最多的情況。那麼可以將所有的數量都隨機分配給這幾種元素(因為乘以相同的因子)。例如,有三種元素數量最多且一樣,則方案數為3^n(n個位置可以隨機分配三種元素中的一種)。

程式碼:

#include <iostream>
#include <string>
#include <cstdio>
#include <algorithm>
#define LL long long
#define mod 1000000007
using namespace std;
int a[4];
bool cmp(int a,int b)
{
	return a>b;
}
int main()
{
	int n,factor;
	LL ans=1;
	string s;
	cin>>n>>s;
    for(int i=0;i<n;i++)
	{
		if(s[i]=='A')
			a[0]++;
		else if(s[i]=='C')
			a[1]++;
		else if(s[i]=='G')
			a[2]++;
		else
			a[3]++;
	}
	sort(a,a+4,cmp);
	if(a[0]!=a[1])
		ans=1;
	else
	{
		if(a[1]!=a[2])
		{
            factor=2;
		}
		else
		{
			if(a[2]!=a[3])
				factor=3;
			else 
				factor=4;
		}
		for(int i=0;i<n;i++)
			ans=ans*factor%mod;
	}
	cout<<ans<<endl;
	return 0;
}