【打CF,学算法——二星级】CodeForces 520C DNA Alignment (构造)
2016-07-08 09:54
393 查看
【CF简介】
提交链接:CF 520C
题面:
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
Output
Input
Output
Input
Output
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个位置可以随机分配三种元素中的一种)。
代码:
提交链接:CF 520C
题面:
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; }
相关文章推荐
- JavaScript构造函数详解
- jQuery入门 构造函数
- js调用webservice构造SOAP进行身份验证
- 【经验总结】编写JavaScript代码时应遵循的14条规律
- 使用Java构造和解析Json数据的两种方法(详解二)
- 使用Java构造和解析Json数据的两种方法(详解一)
- java中的静态代码块、构造代码块、构造方法详解
- Python类方法__init__和__del__构造、析构过程分析
- 为什么我们不能坚持到底?
- 关于获得ArrayAdapter对象的细节
- nVIDIA显卡命名规律
- 类的继承与派生
- 人才成长有其自身的规律
- 探讨“临时对象”(temporary object)
- Codeforces Round #197 (Div. 2)
- Codeforces Round #198 (Div. 1)
- Codeforces 405E Codeforces Round #238 (Div. 2)E
- Codeforces 407C Codeforces Round #239 (Div. 1)C
- CodeForces 449A - Jzzhu and Chocolate
- CodeForces 449 B. Jzzhu and Cities