Codeforces 164B || Codeforces 163A
来源:互联网 发布:java interface 泛型 编辑:程序博客网 时间:2024/06/06 15:42
Codeforces 164B
是个字符串问题(codeforces把这类问题归为two pointers),给了2个字符串a,b都可以任意移位,且b中无相等元素,满足条件“是a的子串==是b的子序列”的最大的串
#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;int p[1000010],a[1000010],n,m,ans;deque<int>q;bool in(int x,int L,int R){ if(L<=R)return L<=x&&x<=R; else return x<=R||L<=x;}int main(){ scanf("%d%d",&n,&m); for(int i=0;i<n;++i)scanf("%d",&a[i]); memset(p,-1,sizeof p); for(int i=0,x;i<m;++i){ scanf("%d",&x); p[x]=i; // 利用串里无相同元素 } for(int i=0;i<n+n;++i){ if(p[a[i%n]]==-1){ q.clear(); continue; } while(!q.empty()&&in(p[a[i%n]],q.front(),q.back())) //in函数判断p[a[i%n]]必须在选出的b序列的外面,否则的话队首元素出队q.pop_front(); q.push_back(p[a[i%n]]); ans=max(ans,(int)q.size());if(ans>=min(m,n))break; } printf("%d\n",ans); return 0;}
Codeforces 163A
(x,y)其中x==y,x为字符串a的子串,y为字符串b的子序列,问这样的序偶有多少个?
解:这是个DP的问题
The problem could be solved with the following dynamic programming. Let f[i, j] be the number of distinct pairs (“substring starting at positioni” and “subsequence of the substring t[j… |t|]”)
Then:
f[i, j] = f[i, j + 1];if (s[i] == t[j]) add(f[i, j], f[i + 1, j + 1] + 1)
Answer = f[i,0]
#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;#define mod 1000000007int dp[5010][5010];int main(){int i,j;char s1[5010],s2[5010];scanf("%s%s",s1,s2);int len1=strlen(s1),len2=strlen(s2);for(i=len1-1;i>=0;i--)for(j=len2-1;j>=0;j--){if(s1[i]==s2[j])dp[i][j]=(dp[i][j+1]+1+dp[i+1][j+1])%mod;elsedp[i][j]=(dp[i][j+1])%mod;}int sum=0;for(i=len1-1;i>=0;i--)sum=(sum+dp[i][0])%mod;printf("%d\n",sum);}
- Codeforces 164B || Codeforces 163A
- codeforces #334A,B
- CodeForces 558A,B
- codeforces #341(A,B)
- codeforces 282 A B C
- Codeforces 300-A/B/C
- CodeForces 560A,B,C
- CodeForces 572A,B,C
- codeforces#336 div2 A,B
- Codeforces 701A+701B
- CodeForces 707-A,B,C
- Codeforces 371 A,B,C
- codeforces 667A 667B
- Codeforces Round #424(A+B)
- B - Making a String-----CodeForces 624B
- Codeforces Educational Round #18(Codeforces 792 A B C D)
- Codeforces-Educational Codeforces Round 32-(A,B,C,D)
- codeforces B
- 第十周实验报告(任务1)
- GDT,LDTH和UuserContex关系
- apache2 与 IIS端口冲突的解决
- StretchDIBits用法
- AIX系统日常巡检步骤
- Codeforces 164B || Codeforces 163A
- “初始化”一则
- .net连接MySQL数据库
- 编译u-boot遇到的问题和解决办法
- Ubuntu下PATH设置
- 医院健康体检系统
- android发送消息正确方式
- wTL
- android Activity中启动另一应用程序的方法,无需得到类名