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 function h(s, t) is defined as the number of positions in which the respective symbols of s and t are the same. Functionh(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 tthere 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

 

题目的意思是,知道 s ,求使ρ(s,t) 最大的t有多少种。  很显然, t 中的字母应该只含有 s 中出现最多的字母。例如s 为 AATTC,那么t 可以为

AAAAA  TTTTT  AATTT AAAAT 。。。。。。等等。所以t 的种数为 s种出现最多的字母的个数 p的n次幂

看代码

 

#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <vector>
#include <sstream>
#include <cstdlib>
#include <complex>
#include <cstring>
#include <iostream>
#include <algorithm>

#define REP(i,N) for (int i = 0;i < (N);i++)
#define REP_1(i,N) for (int i = 1;i < (N);i++)
#define REP_2(i,be,en) for (int i = (be);i < (en);i++)
#define DWN(i,N) for (int i = (N);i >= 0;i--)
#define DWN_1(i,N) for (int i = (N);i >= 1;i--)
#define DWN_2(i,en,be) for (int i = (en);i >= (be);i--)
#define FR(N) freopen((N),"r",stdin)
#define FW(N) freopen((N),"w",stdout)
#define GETS(ch) fgets((ch),MAXN,stdin)
#define INF 0x3f3f3f3f
#define MAXN 110
#define MOD 1000000007
using namespace std;

typedef long long LL;
typedef map<char,int> MINT;
typedef vector<int> VINT;
typedef set<char> SINT;

int quick(int a,int b) {
	LL ans = 1,x = a;
	while (b) {
		if (b & 1) ans = ans * x % MOD;
		x = x * x % MOD;
		b >>= 1;
	}
	return ans;
}

int main() {
	int N;
	string str;
	while (cin >> N) {
		cin >> str;
		int maxn = 0;
		MINT M;
		REP(i,N) {
			M[str[i]]++;
			maxn = max(maxn,M[str[i]]);
		}
		int pos = 0;
		MINT :: iterator Iter;
		for (Iter = M.begin();Iter != M.end();Iter++) {
			if (Iter -> second == maxn) {
				pos++;
			}
		}
		cout << quick(pos,N) << endl;
	}
}

 

  

 

 

posted @ 2015-03-02 18:25  闪光阳  阅读(290)  评论(0编辑  收藏  举报