CodeForces - 520C DNA Alignment(思维)
生活随笔
收集整理的這篇文章主要介紹了
CodeForces - 520C DNA Alignment(思维)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:點擊查看
題目大意:給出定義函數h(s,t),其值等于字符串s和字符串t中相同字符的位置的個數,再給出一個函數p(s,t),簡單來說就是一個二重循環,每次讓字符串的第一個元素移到最后一個元素,現有s和t兩個字符串然后共有n*n種匹配情況的函數h,現在給出一個字符串s,問有能構造出多少種t,滿足p(s,t)達到最大值
題目分析:這個題目的題意比較難讀懂,讀懂了之后也不好想。。最后我是看網上的題解才勉強能想出這個題的
我們在已知字符串s的情況下,設在t中構造了一個字符‘A’,那么字符‘A’在函數p(s,t)種的貢獻是 (A在s中的數量)*n
則為了讓p(s,t)盡量大,因為n是不能改變的,所以我們需要用s中出現最多的字符來補全字符串t,若只有一種字符出現的次數最多,那么字符串t的每個位置都可以有1種方式補全,那么答案就是,若有兩種字符出現的次數都是最多的,那么字符串t的每個位置都可以有兩種方式補全,那么答案就是,依次類推,我們只需要計算出有多少種出現次數最多的字符即可
代碼:
#include<iostream> #include<cstdlib> #include<string> #include<cstring> #include<cstdio> #include<algorithm> #include<climits> #include<cmath> #include<cctype> #include<stack> #include<queue> #include<list> #include<vector> #include<set> #include<map> #include<sstream> #include<unordered_map> using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=1e6+100;const int mod=1e9+7;int cnt[30]={0};LL q_pow(LL a,LL b) {LL ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod;b>>=1;}return ans; }int main() { // freopen("input.txt","r",stdin);int n;scanf("%d",&n);string s;cin>>s;int mmax=-inf;for(int i=0;i<s.size();i++)cnt[s[i]-'A']++;for(int i=0;i<26;i++)mmax=max(mmax,cnt[i]);int num=0;for(int i=0;i<26;i++)if(mmax==cnt[i])num++;printf("%lld\n",q_pow(num,n));return 0; }?
總結
以上是生活随笔為你收集整理的CodeForces - 520C DNA Alignment(思维)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 凸包模板(Graham算法)
- 下一篇: HDU - 1536 S-Nim(sg函