Codeforces Beta Round #17 C. Balance DP

C. Balance

題目鏈接

http://codeforces.com/contest/17/problem/C

題面

Nick likes strings very much, he likes to rotate them, sort them, rearrange characters within a string... Once he wrote a random string of characters a, b, c on a piece of paper and began to perform the following operations:

to take two adjacent characters and replace the second character with the first one,
to take two adjacent characters and replace the first character with the second one
To understand these actions better, let's take a look at a string ?abc?. All of the following strings can be obtained by performing one of the described operations on ?abc?: ?bbc?, ?abb?, ?acc?. Let's denote the frequency of a character for each of the characters a, b and c as the number of occurrences of this character in the string. For example, for string ?abc?: |a| = 1, |b| = 1, |c| = 1, and for string ?bbc?: |a| = 0, |b| = 2, |c| = 1.

While performing the described operations, Nick sometimes got balanced strings. Let's say that a string is balanced, if the frequencies of each character differ by at most 1. That is ?-?1?≤?|a|?-?|b|?≤?1, ?-?1?≤?|a|?-?|c|?≤?1 и ?-?1?≤?|b|?-?|c|?≤?1.

Would you help Nick find the number of different balanced strings that can be obtained by performing the operations described above, perhaps multiple times, on the given string s. This number should be calculated modulo 51123987.

輸入

The first line contains integer n (1?≤?n?≤?150) — the length of the given string s. Next line contains the given string s. The initial string can be balanced as well, in this case it should be counted too. The given string s consists only of characters a, b and c.

輸出

Output the only number — the number of different balanced strings that can be obtained by performing the described operations, perhaps multiple times, on the given string s, modulo 51123987.

樣例輸入

4
abca

樣例輸出

7

題意

你可以使得一個元素變成他周圍的元素的顏色,可以改變無數次,現在給你一個串,問你一共有多少種方案,使得a和b和c的個數相差不超過1

題解

dp[i][a][b][c],表示考慮到第i個位置,當前有a個a,b個b,c個c 的方案數

然后轉移就好了

維護一個next[i][3]表示下一個在哪兒。

雖然是4維dp,但是卻是150 50 50 50 的

代碼

#include<bits/stdc++.h>
using namespace std;
const int mod = 51123987;
int dp[152][52][52][52],n,nxt[152][3];
string s;
void add(int &a,int b){a = a+b;if(a>=mod)a%=mod;
}
int main()
{scanf("%d",&n);cin>>s;for(int j=0;j<3;j++)nxt[n][j]=n;for(int i=n-1;i>=0;i--){for(int j=0;j<3;j++)nxt[i][j]=nxt[i+1][j];nxt[i][s[i]-'a']=i;}dp[0][0][0][0]=1;int ans = 0;for(int i=0;i<n;i++){for(int a=0;a*3<=n+2;a++){for(int b=0;b*3<=n+2;b++){for(int c=0;c*3<=n+2&&a+b+c<=n;c++){if(dp[i][a][b][c]){if(a+b+c==n&&abs(b-c)<=1&&abs(a-c)<=1&&abs(b-c)<=1)add(ans,dp[i][a][b][c]);add(dp[nxt[i][0]][a+1][b][c],dp[i][a][b][c]);add(dp[nxt[i][1]][a][b+1][c],dp[i][a][b][c]);add(dp[nxt[i][2]][a][b][c+1],dp[i][a][b][c]);}}}}}printf("%d\n",ans);
}

轉載于:https://www.cnblogs.com/qscqesze/p/6173531.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/253771.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/253771.shtml
英文地址,請注明出處:http://en.pswp.cn/news/253771.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

時鐘切換處理(Verilog)

隨著各種應用場景的限制&#xff0c;芯片在運行時往往需要在不同的應用下切換不同的時鐘源&#xff0c;例如低功耗和高性能模式就分別需要低頻率和高頻率的時鐘。兩個時鐘源有可能是同源且同步的&#xff0c;也有可能是不相關的。直接使用選擇邏輯進行時鐘切換大概率會導致分頻…

SSH整合中,使用父action重構子類action類.(在父類中獲取子類中的泛型對象)

import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type;import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven;/*** 文件名 : BaseAction.java* 提取SSH中的action類* 由于SSH的action中采用模型驅動的方法,使用泛…

用BusyBox制作Linux根文件系統

STEP 1&#xff1a;構建目錄結構 創建根文件系統目錄&#xff0c;主要包括以下目錄 /dev /etc /lib /usr /var /proc /tmp /home /root /mnt /bin /sbin /sys #mkdir /home/rootfs #cd /home/rootfs #mkdir dev etc lib usr var proc tmp home roo…

Angular Elements 組件在非angular 頁面中使用的DEMO

2019獨角獸企業重金招聘Python工程師標準>>> 一、Angular Elements 介紹 Angular Elements 是伴隨Angular6.0一起推出的新技術。它借助Chrome瀏覽器的ShadowDom API&#xff0c;實現一種自定義組件。 這種組件可以用Angular普通組件的開發技術進行編寫&#xff0c;…

(轉) android里,addContentView()動態增加view控件,并實現控件的頂部,中間,底部布局...

http://blog.csdn.net/bfboys/article/details/52563089轉載于:https://www.cnblogs.com/zhangminghan/p/6182909.html

verilog仿真——$test$plusargs 和 $value$plusargs

VERILOG的參數可以用define和parameter的方式定義&#xff0c;這種方法要求我們在編譯前將變量必須定義好&#xff0c;編譯完成之后再也不能修改&#xff1b; 然而&#xff0c;有時候我們在進行仿真時&#xff0c;需要從外部傳遞參數&#xff0c;這個要求怎么滿足呢&#xff1…

盧卡斯定理

盧卡斯定理:解決一類組合數取模問題 A、B是非負整數&#xff0c;p是質數。AB寫成p進制&#xff1a;Aa[n]a[n-1]...a[0]&#xff0c;Bb[n]b[n-1]...b[0]。 則組合數C(A,B)與C(a[n],b[n])*C(a[n-1],b[n-1])*...*C(a[0],b[0]) modp同余 即&#xff1a;Lucas(n,m,p)c(n%p,m%p)*Luc…

內核理解

在純技術方面&#xff0c;內核是硬件與軟件之間的一個中間層。其作用是將應用程序的請求傳遞給硬件&#xff0c;并充當底層的驅動程序&#xff0c;對系統中的各種設備和組件。內核啟動init程序作為第一個進程&#xff0c;該進程負責進一步的系統初始化操作&#xff0c;并顯示登…

loadrunner中對https證書的配置

1、準備好網站的證書&#xff0c;一般證書是cer格式&#xff1b; 2、因為loadrunner只支持pem格式的證書&#xff0c;所以要將證書轉換格式&#xff0c;利用openssl工具&#xff1b;&#xff08;或者直接讓開發提供pem格式的證書&#xff09;3、得到pem格式的證書之后&#xff…

Android 9 Pie震撼來襲 同步登陸WeTest

作者&#xff1a;We Test小編商業轉載請聯系騰訊WeTest獲得授權&#xff0c;非商業轉載請注明出處。原文鏈接&#xff1a;wetest.qq.com/lab/view/40…WeTest 導讀2018年8月7日&#xff0c;Google對外發布最新 Android 9.0 正式版系統&#xff0c;并宣布系統版本Android P 被正…

Datapath綜合代碼規范(Verilog)

一、一般準則 1、有符號數運算 利用類型“signed”完成有符號數運算&#xff0c;而不是用無符號數模擬有符號數運算。這樣可以得到更好的QoR。在資源報告中檢查操作數的類型和大小。 2、符號/零擴展 盡量不要手動擴展。verilog利用signed/unsigned會自動完成擴展。這樣代碼可…

Linux下V4L2編程小結

http://www.360doc.com/content/12/0318/16/532901_195392228.shtml :davind dm365linux移植 http://www.embedhq.org/html/jsbw/2010/0425/390.html :Linux下V4L2編程小結

百(垃)度(圾)之星初賽B hdu6114

Chess 題意&#xff1a;中文題 思路&#xff1a;其實就是在n個格子上放m個棋子&#xff08;n>m&#xff09;&#xff08;xjb套Lucas的板子... AC代碼&#xff1a; #include "iostream" #include "string.h" #include "stack" #include "…

variable 'xxx' unsafe in 'case'的處理

問題描述&#xff1a; case get(?Player_LoopTaskInfo) of{TargetCnt, TaskStar, TaskExp} ->ok;_ ->throw("not_found_loop_task_info") end 在case語句中&#xff0c;這樣寫&#xff0c;編譯時&#xff0c;會提示變量unsafe&#xff0c;解決編譯器報錯的…

SDUT 3347 數據結構實驗之數組三:快速轉置

數據結構實驗之數組三&#xff1a;快速轉置 Time Limit: 1000 ms Memory Limit: 65536 KiBProblem Description 轉置運算是一種最簡單的矩陣運算&#xff0c;對于一個m*n的矩陣M( 1 < m < 10000,1 < n < 10000 )&#xff0c;它的轉置矩陣T是一個n*m的矩陣&…

linux設備和驅動加載的先后順序

Linux驅動先注冊總線&#xff0c;總線上可以先掛device&#xff0c;也可以先掛driver&#xff0c;那么究竟怎么控制先后的順序呢。 Linux系統使用兩種方式去加載系統中的模塊&#xff1a;動態和靜態。 靜態加載&#xff1a;將所有模塊的程序編譯到Linux內核中&#xff0c;由do_…

CMOS 圖像傳感器——Skipping 和 Binning 模式

在通常的CMOS讀取方式中&#xff0c;由于像素讀取規模的差異&#xff0c;不同的分辨率對應不同的幀率。在通道帶寬固定的前提下&#xff0c;想要提高幀率就要考慮是否需要縮小視野&#xff08;外圈裁切&#xff09;。若不希望視野縮小&#xff0c;需要減少采樣的分辨率。 常用的…

DAVINCI DM365-368中 linux-2.6.32的移植

http://www.360doc.com/content/12/0318/16/532901_195392228.shtml 很詳細的一篇文章&#xff0c;在此感謝了&#xff01; http://www.rosoo.net/a/201001/8316.html DM系列芯片外設詳細介紹

Jacoco--測試覆蓋率工具

介紹JaCoCo&#xff08;Java Code Coverage&#xff09;是一種分析單元測試覆蓋率的工具&#xff0c;使用它運行單元測試后&#xff0c;可以給出代碼中哪些部分被單元測試測到&#xff0c;哪些部分沒有沒測到&#xff0c;并且給出整個項目的單元測試覆蓋情況百分比&#xff0c;…

HTML 標記大全參考手冊

1.文件結構 文件類型 <HTML></HTML> &#xff08;放在文檔的開頭與結尾&#xff09; 文件主題 <TITLE></TITLE> &#xff08;必須放在「文頭」區塊內&#xff09; 文頭 <HEAD></HEAD> &#xff08;描述性資料&#xff0c;如「主題」&#…