HDU-5763-Another Meaning-hash加DP
来源:互联网 发布:linux php 提权 编辑:程序博客网 时间:2024/04/29 18:39
题目大致意思:给你一个字符串A,一个字符串B,B字符串代表有两种意思的字符串。问A可能有多少种意思
思路:用hash处理一下看A中哪些位置是B字符串匹配的结尾。然后DP
dp[i]=dp[i-1]+dp[i-m](i是B匹配的结尾)
dp[i]=dp[i-1](不是结尾)
注意当i-m<0时 dp[i-m]=dp[0];
#include<bits/stdc++.h>using namespace std;#define nc 131#define mod 1000000007unsigned long long Pow[111111];unsigned long long Hash[111111];long long dp[111111];char a[111111];char b[111111];void inti(void) { Pow[0]=1; for(int i=1;i<100005;i++) Pow[i]=Pow[i-1]*nc;}int main(){ int t; int kase=0; inti(); scanf("%d",&t); while(t--) { scanf("%s",a); scanf("%s",b); int la=strlen(a); int lb=strlen(b); unsigned long long fb=0; for(int i=0;i<lb;i++) { fb=fb*nc+b[i]-'a'+1; } // cout <<"fb" <<fb << endl; Hash[0]=a[0]-'a'+1; for(int i=1;i<lb;i++) { Hash[i]=Hash[i-1]*nc+a[i]-'a'+1; } for(int i=lb;i<la;i++) { Hash[i]=(Hash[i-1]-Pow[lb-1]*(a[i-lb]-'a'+1))*nc+a[i]-'a'+1; } /* for(int i=0;i<la;i++) cout << hash[i] << " "; cout << endl; */ if(Hash[0]!=fb) { dp[0]=1; } else { dp[0]=2; } for(int i=1;i<la;i++) { if(Hash[i]==fb) { if(i>=lb) dp[i]=(dp[i-1]+dp[i-lb])%mod; else dp[i]=(dp[i-1]+dp[0])%mod; } else { dp[i]=dp[i-1]; } } printf("Case #%d: %lld\n",++kase,dp[la-1]); } return 0;}
0 0
- HDU-5763-Another Meaning-hash加DP
- DP-HDU-5763-Another Meaning
- HDU 5763 Another Meaning (DP)
- HDU 5763 Another Meaning (DP)
- HDU-5763-Another Meaning(KMP+DP)
- Hdu-5763 Another Meaning(DP+KMP)
- HDU 5763(Another Meaning kmp+dp)
- HDU 5763 Another Meaning(KMP+DP)
- HDU 5763 Another Meaning dp+kmp优化
- HDU 5763 Another Meaning 多校赛 (DP + KMP)
- HDU 5763 Another Meaning(kmp+dp)
- HDU 5763 Another Meaning(KMP+DP)
- hdu 5763 Another Meaning kmp+dp
- HDU Another Meaning DP+KMP
- HDU 5763 Another Meaning
- HDU 5763 Another Meaning
- HDU 5763 Another Meaning
- 【HDU】5763 Another Meaning
- spark实战:对日志进行查询
- POJ3420 Quad Tiling
- 从头说12种排序算法:原理、图解、动画视频演示、代码以及笔试面试题目中的应用
- 问题锦囊之WS_MOBILE_PAY_SDK_BASE
- Android子线程中更新UI的3种方法
- HDU-5763-Another Meaning-hash加DP
- 一种基于TS包比例占用的固定码率TS流复用算法
- POJ2524 并差集以及优化(路径压缩+按秩合并)的简洁介绍
- JAVA基础----代码块的执行顺序
- hdu2571命运dfs
- 存储过程
- Hibernate注解使用以及Spring整合
- linux基础——linux下线程池的编程实现及注意事项
- HDU 1233还是畅通工程【Prim】