uvalive 6932
三个串必须要一起dp
之前刚学了dfs的记忆化搜索的dp方式 觉得很舒服 现学现卖然后两个小时都没有做出来
优化1:之前在dfs中 对每一个pos都会枚举所有可能的组合 结合当前状态来产生新的状态 来决定接下来是直接算还是继续dfs
枚举的结果 只与当前状态和for的区间有关 所以提前预处理出每种情况的结果
优化完跑了个长2W的全?序列 在跑到pos=1W3左右的时候崩溃 手动加栈依然在这里崩溃 猜想是栈崩了。。
优化2:进行完优化1后代码变得好看多了 发现在每一步的结果只与下一步有关 还很严格 于是可以搞一个dp[zt][pos]出来 根据上一个优化算的状态数组直接从pos+1推出来
感想:dfs写dp是挺舒服 但是得是特定的 像数位dp就会很舒服 普通dp就不一定 大概是我接受了for写dp的设定
dfs写dp得注意深度。。之前写的dp才300层。。1e6的再敢写就剁手
for循环计数有时候可以用很舒服的预处理给做了
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<vector>
#include<queue>
#include<map>
#include<string>
#include<iostream>
#include<algorithm>
#include<stack>
#include<set>
using namespace std;
#define L long long
#define pb push_back
#define lala printf("--------\n");
#define ph push
#define rep(i, a, b) for (L i=a;i<=b;++i)
#define dow(i, b, a) for (L i=b;i>=a;--i)
#define fmt(i,n) if(i==n)printf("\n");else printf(" ") ;
#define rnode(i,u) for(L i = head[u] ; i != -1 ; i = b[i].nex)
#define fi first
#define se second
template<class T> inline void flc(T &A, L x){memset(A, x, sizeof(A));}
L read(){L x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;} const L mod = 1e9+9 ; char a[1000050] ;
char b[1000050] ;
char c[1000050] ;
L la,lb,lc;
L dp[3][1000050] ;
L G[28][28][28][5][5] ;
void init() {
flc(G,0) ;
rep(aa,0,27) rep(bb,0,27) rep(cc,0,27) {
L La=1,Ra=26;
L Lb=1,Rb=26;
L Lc=1,Rc=26;
if(aa!=27)La=Ra=aa;
if(bb!=27)Lb=Rb=bb;
if(cc!=27)Lc=Rc=cc;
L (&g)[5][5] = G[aa][bb][cc] ;
rep(a,La,Ra)
rep(b,Lb,Rb)
rep(c,Lc,Rc){
g[1][2] += a<b&&b<c ;
g[1][1] += a==b&&b==c ;
g[1][3] += a<b&&b==c ;
g[1][4] += a==b&&b<c ;
g[3][2] += b<c ;
g[3][3] += b==c ;
g[4][2] += a<b ;
g[4][4] += a==b ;
g[2][2] ++ ;
}
}
}
L jfhsb[1000050][5] ;
L zhrsb() {
flc(jfhsb,0) ;
dow(pos,la,1) {
rep(zt,1,4){
L aa,bb,cc;
if(a[pos]=='?')aa=27;else aa=a[pos]-'a'+1;
if(b[pos]=='?')bb=27;else bb=b[pos]-'a'+1;
if(c[pos]=='?')cc=27;else cc=c[pos]-'a'+1;
L ztt2 = (((((dp[0][pos] * dp[1][pos])) % mod ) * dp[2][pos])%mod) ;
L ztt1 = jfhsb[pos+1][1]%mod ;
L ztt3 = jfhsb[pos+1][3]%mod ;
L ztt4 = jfhsb[pos+1][4]%mod ;
L zt1 = (ztt1 * G[aa][bb][cc][zt][1])%mod ;
L zt2 = (ztt2 * G[aa][bb][cc][zt][2])%mod ;
L zt3 = (ztt3 * G[aa][bb][cc][zt][3])%mod ;
L zt4 = (ztt4 * G[aa][bb][cc][zt][4])%mod ;
jfhsb[pos][zt] = (((((zt1+zt2)%mod)+zt3)%mod)+zt4) % mod;
}
}
return jfhsb[1][1] ;
} int main () {
init() ;
L t ;
scanf("%lld" , &t) ;
while(t--){
flc(dp,0) ;
scanf("%s",a+1);
scanf("%s",b+1);
scanf("%s",c+1);
la = strlen(a+1) ; lb = strlen(b+1) ; lc = strlen(c+1) ;
L maxx = max(la,max(lb,lc)) ;
rep(i,la+1,maxx) a[i] = 'a' - 1 ;
rep(i,lb+1,maxx) b[i] = 'a' - 1 ;
rep(i,lc+1,maxx) c[i] = 'a' - 1 ;
a[maxx+1] = '\0' ;
b[maxx+1] = '\0' ;
c[maxx+1] = '\0' ;
la = lb = lc = maxx ;
L wh = 1 ;
for(L i=la;i>=1;i--){
if(a[i]=='?'){
wh %= mod ;
dp[0][i] = wh ;
wh *= 26 ;
wh %= mod ;
}
else {
dp[0][i] = wh ;
}
}
wh = 1 ;
for(L i=lb;i>=1;i--){
if(b[i]=='?'){
wh %= mod ;
dp[1][i] = wh ;
wh *= 26 ;
wh %= mod ;
}
else {
dp[1][i] = wh ;
}
}
wh = 1 ;
for(L i=lc;i>=1;i--){
if(c[i]=='?'){
wh %= mod ;
dp[2][i] = wh ;
wh *= 26 ;
wh %= mod ;
}
else {
dp[2][i] = wh ;
}
}
L ans = zhrsb() ;
printf("%lld\n" , ans) ;
}
}
uvalive 6932的更多相关文章
- UVALive - 4108 SKYLINE[线段树]
UVALive - 4108 SKYLINE Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug ...
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- 思维 UVALive 3708 Graveyard
题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...
- UVALive 6145 Version Controlled IDE(可持久化treap、rope)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- UVALive 6508 Permutation Graphs
Permutation Graphs Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit ...
- UVALive 6500 Boxes
Boxes Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Pract ...
- UVALive 6948 Jokewithpermutation dfs
题目链接:UVALive 6948 Jokewithpermutation 题意:给一串数字序列,没有空格,拆成从1到N的连续数列. dfs. 可以计算出N的值,也可以直接检验当前数组是否合法. # ...
- 【暑假】[实用数据结构]UVAlive 3135 Argus
UVAlive 3135 Argus Argus Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %l ...
随机推荐
- activity通过流程实例id动态获取流程图并展示在jsp页面上
提供的Service方法如下: Java /** * 获取当前任务流程图 * * @param processInstanceId * @return */ @Override public Inpu ...
- Codeforces Round #324 (Div. 2) (快速判断素数模板)
蛋疼的比赛,当天忘了做了,做的模拟,太久没怎么做题了,然后C题这么简单的思路却一直卡到死,期间看了下D然后随便猜了下,暴力了下就过了. A.找一个能被t整除的n位数,那么除了<=10以外,其他都 ...
- 将DataTable转换成Json格式
方法一: 将DataTable数据拼接成json字符串,方法如下: ///<summary> /// dataTable转换成Json格式 ///</summary> ///& ...
- 巨蟒django之权限7:动态生成一级&&二级菜单
内容回顾: . 权限的控制 . 表结构设计 存权限的信息 用户表 - name 用户名 - pwd 密码 - roles 多对多 角色表 - name - permissions 多对多 权限表 - ...
- 字符串 转 时间戳 unix_timestamp('2018-07-01 0:0:0')
SELECT FROM_UNIXTIME(createtime,'%Y%m%d') AS d, s.app,SUM(o.val) FROM orders o LEFT JOIN app_type s ...
- 如何使用 Opencv dnn 模块调用 Caffe 预训练模型?
QString modelPrototxt = "D:\\Qt\\qmake\\CaffeModelTest\\caffe\\lenet.prototxt"; QString mo ...
- curl学习总结
1.接口 function interface($postfields=array(),$url){ //设置post请求HTTP头字段的数组 $httpheader ...
- PopuWindow和软件盘共存时的设置
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/mingyue_1128/article/details/32316069 一.键盘不消失,popuw ...
- 分布式计算开源框架Hadoop入门实践(三)
Hadoop基本流程 一个图片太大了,只好分割成为两部分.根据流程图来说一下具体一个任务执行的情况. 在分布式环境中客户端创建任务并提交. InputFormat做Map前的预处理,主要负责以下工作: ...
- MySQL数据库(1)_MySQL数据库介绍与安装
一.数据库相关概念的简介 数据库(database,DB)是指长期存储在计算机内的,有组织,可共享的数据的集合.数据库中的数据按一定的数学模型组织.描述和存储,具有较小的冗余,较高的数据独立性和易扩展 ...