Codeforences 17C Balance
来源:互联网 发布:手机ui设计软件 编辑:程序博客网 时间:2024/05/28 05:15
数组,开小了,暴力调了2小时才发现。。。。。
【AC代码】
////Created by just_sort 2016/12/13//Copyright (c) 2016 just_sort.All Rights Reserved//#include <ext/pb_ds/assoc_container.hpp>#include <ext/pb_ds/tree_policy.hpp>#include <ext/pb_ds/hash_policy.hpp>#include <set>#include <map>#include <queue>#include <stack>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <sstream>#include <iostream>#include <algorithm>using namespace std;using namespace __gnu_pbds;typedef long long LL;typedef pair<int, LL> pp;#define MP(x,y) make_pair(x,y)#define PI acos(-1)const int maxn = 1020;const int maxm = 1<<12;const int inf = 0x3f3f3f3f;typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>order_set;//headconst int mod = 51123987;int n, cnt, dp[200][55][55][55], nex[200][3];char s1[200], s2[200];void add(int &x, int y){ (x += y) %= mod; if(x < 0) x += mod;}int main(){ while(scanf("%d", &n) != EOF){ scanf("%s", s1); memset(dp, 0, sizeof(dp)); memset(nex, -1, sizeof(nex)); cnt = 0; s2[cnt++] = s1[0]; for(int i = 1; i < n; i++){ if(s1[i] != s1[i-1]){ s2[cnt++] = s1[i]; } } for(int i = 0; i < cnt; i++){ for(int j = i; j < cnt; j++){ if(s2[j] == 'a' && nex[i][0] == -1){ nex[i][0] = j; } if(s2[j] == 'b' && nex[i][1] == -1){ nex[i][1] = j; } if(s2[j] == 'c' && nex[i][2] == -1){ nex[i][2] = j; } } } // int m = n / 3 + 1; dp[0][0][0][0] = 1; for(int i = 1; i <= cnt; i++){ for(int ca = 0; ca <= m; ca++){ for(int cb = 0; cb <= m; cb++){ for(int cc = 0; cc <= m; cc++){ if(nex[i-1][0] != -1){ //add(dp[nex[i-1][0]][ca+1][cb][cc], dp[i-1][ca][cb][cc]); dp[nex[i-1][0]][ca+1][cb][cc] += dp[i-1][ca][cb][cc]; dp[nex[i-1][0]][ca+1][cb][cc] %= mod; } if(nex[i-1][1] != -1){ //add(dp[nex[i-1][1]][ca][cb+1][cc], dp[i-1][ca][cb][cc]); dp[nex[i-1][1]][ca][cb+1][cc] += dp[i-1][ca][cb][cc]; dp[nex[i-1][1]][ca][cb+1][cc] %= mod; } if(nex[i-1][2] != -1){ //add(dp[nex[i-1][2]][ca][cb][cc+1], dp[i-1][ca][cb][cc]); dp[nex[i-1][2]][ca][cb][cc+1] += dp[i-1][ca][cb][cc]; dp[nex[i-1][2]][ca][cb][cc+1] %= mod; } } } } } int ans = 0; if(n % 3 == 0){ for(int i = 0; i < cnt; i++){ //add(ans, dp[i][n/3][n/3][n/3]); ans += dp[i][n/3][n/3][n/3]; ans %= mod; } } else if(n % 3 == 1){ for(int i = 0; i < cnt; i++){// add(ans, dp[i][n/3+1][n/3][n/3]);// add(ans, dp[i][n/3][n/3+1][n/3]);// add(ans, dp[i][n/3][n/3][n/3+1]); ans += dp[i][n/3+1][n/3][n/3]; ans %= mod; ans += dp[i][n/3][n/3+1][n/3]; ans %= mod; ans += dp[i][n/3][n/3][n/3+1]; ans %= mod; } } else{ for(int i = 0; i < cnt; i++){// add(ans, dp[i][n/3+1][n/3+1][n/3]);// add(ans, dp[i][n/3+1][n/3][n/3+1]);// add(ans, dp[i][n/3][n/3+1][n/3+1]); ans += dp[i][n/3+1][n/3+1][n/3]; ans %= mod; ans += dp[i][n/3+1][n/3][n/3+1]; ans %= mod; ans += dp[i][n/3][n/3+1][n/3+1]; ans %= mod; } } ans = ans % mod; if(ans < 0) ans += mod; printf("%d\n", ans); } return 0;}
0 0
- Codeforences 17C Balance
- Codeforences #309 C(div2)
- Codeforences #334 C(div2)
- Codeforences #345 C
- Codeforences 17E Palisection
- CodeForces 17C Balance (DP)
- CodeForces - 17C Balance(DP)
- Codeforences #307(div2) C . GukiZ hates Boxes
- Codeforences Educational Round10 C. Foe Pairs
- Codeforces Beta Round #17 C. Balance DP
- Codeforces Beta Round #17 C. Balance DP
- Codeforences Goodbye 2015 C. New Year and Domino
- Codeforences #297 div2 (A,B,C,D,E)
- Codeforces Beta Round #17 C. Balance (字符串计数 dp)
- Balance
- balance
- Balance
- B. Center Alignment codeforences
- 【每日一记】设计模式——桥接模式
- c 结构体链表形式
- HDU 1175 连连看(BFS)
- Java I/O类库基本结构
- 文章标题
- Codeforences 17C Balance
- java堆详解
- 上传文件或图片 controller层
- Linux环境tomcat软链接问题
- (四)工厂方法模式详解(另附简单工厂的死亡之路)
- R链接到数据库(http://www.dataguru.cn/thread-289411-1-1.html)
- 表操作的常用SQL语句-oracle
- 用联合体判断机器是大端字节序还是小端字节序
- mysql-视图