字符串dp(easy)

被3整除的子序列
链接:https://ac.nowcoder.com/acm/problem/21302
来源:牛客网

题目描述
给你一个长度为50的数字串,问你有多少个子序列构成的数字可以被3整除
答案对1e9+7取模
输入描述:
输入一个字符串,由数字构成,长度小于等于50
输出描述:
输出一个整数

基本思路
除三取余只能得到0,1,2;
那就从头开始遍历序列,依次求出除三取余的余数,用dp更新结果三种情况分别是
余数=0
dp[0]=dp[0]+dp[0]+1;
dp[1]=dp[1]+dp[1];
dp[2]=dp[2]+dp[2];
余数=1
dp[0]=dp[0]+dp[2];
dp[1]=dp[1]+dp[0]+1;
dp[2]=dp[2]+d[1];
余数=2
dp[0]=dp[0]+dp[1];
dp[1]=dp[1]+dp[2];
dp[2]=dp[2]+dp[0]+1;
如果直接这样写会取用更新的最新值,所以用几个变量存一下。

#include<bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define ll long long

/*ll qpow(ll a,ll b){
      ll ans=1;
	  while(b){
	  	if(b&1){
	  		ans=ans*a;
		  }
		  a=a*a;
		  b>>=1;
	  }
	  return ans;

}*/

/*const char *a[]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
struct s{
	string sfz;
	int sj,ks;
};*/
//int cmp(s a,s b){
//	return a.pj>b.pj;
//}
/*ll gcd(ll a,ll b) // 求最大公因数
{
    return b==0?a:gcd(b,a%b);
}

ll  lcm(ll a,ll b)
{
    ll k;
    k=a*b/(gcd(a,b));
    return k;
}*/
const int mod=1e9+7;
ll dp[1000008];
int main(){
	string s;
	cin>>s;
	ll m;
	ll m0,m1,m2;
	memset(dp,0,sizeof(dp));
	ll w=s.length();
	
	for(int i=0;i<w;i++){
		m=(s[i]-'0')%3;
		
		dp[m]=dp[m]%mod;
		m0=m1=m2=0;
		if(m==0){
			m0+=dp[0]+1;
			m1+=dp[1];
			m2+=dp[2];
		}
		else if(m==1){
			m0+=dp[2];
			m1+=dp[0]+1;
			m2+=dp[1];
		}
		else if(m==2){
			m0+=dp[1];
			m1+=dp[2];
			m2+=dp[0]+1;
		}
		dp[0]+=m0;
		dp[1]+=m1;
		dp[2]+=m2;
		for(int x=0;x<=2;x++){
			dp[x]=dp[x]%mod;
		}
	}
	cout<<dp[0];
	
} 

用二维dp可以用数组下标记录更加方便

#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
#define ll long long
const int mod=1e9+7;
int main(){
    ll n,m,k,l,w,y;
    char s[55];
    int dp[55][4];
    memset(dp,0,sizeof(dp));
    scanf("%s",s+1);
    l=strlen(s+1);
    dp[1][(s[1]-'0')%3]=1;
    for(int i=2;i<=l;i++){
    m=(s[i]-'0')%3;
    dp[i][m]+=1;
    dp[i][m]%=mod;
    for(int j=0;j<3;j++){
        dp[i][j]+=(dp[i-1][j]+dp[i-1][(j+3-m)%3])%mod;
    }
    }
    cout<<dp[l][0]<<endl;
    }

String Game(吉林省赛)
Clair and Bob play a game. Clair writes a string of lowercase characters, in which Bob sets the puzzle
by selecting one of his favorite subsequence as the key word of the game. But Bob was so stupid that he
might get some letters wrong.
Now Clair wants to know whether Bob’s string is a subsequence of Clair’s string and how many times
does Bob’s string appear as a subsequence in Clair’s string. As the answer may be very large, you should
output the answer modulo 109 + 7.
Input
The first line is Clair’s string (whose length is no more than 5000), and the second line is Bob’s string
(whose length is no more than 1000).
Output
Output one line, including a single integer representing how many times Bob’s string appears as a
subsequence in Clair’s string. The answer should modulo 109 + 7.
题意就是给两个字符串,求第二个字符串在第一个中出现过几次
思路
从前往后遍历clair的字符串
从后往前遍历bob的字符串
dp[i]代表clair前i个字符有dp[j]种子序列等于bob前j个字符

#include<bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define ll long long
const int mod=1e9+7;
/*ll qpow(ll a,ll b){
      ll ans=1;
	  while(b){
	  	if(b&1){
	  		ans=ans*a;
		  }
		  a=a*a;
		  b>>=1;
	  }
	  return ans;
 
}*/
 
/*const char *a[]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
struct s{
	string sfz;
	int sj,ks;
};*/
//int cmp(s a,s b){
//	return a.pj>b.pj;
//}
/*ll gcd(ll a,ll b) // 求最大公因数
{
    return b==0?a:gcd(b,a%b);
}
 
ll  lcm(ll a,ll b)
{
    ll k;
    k=a*b/(gcd(a,b));
    return k;
}*/
 
ll dp[100008];
int main(){
	string s1,s2;
	while(cin>>s1){
		cin>>s2;
		memset(dp,0,sizeof(dp));
		for(int i=0;i<s1.size();i++){
			for(int j=s2.size()-1;j>=0;j--){
				if(s1[i]==s2[j]){
					if(j==0) dp[j]++;
				else {
					dp[j]+=dp[j-1];//s1第i位作为s2的第j位时,前j-1个字符的选择有dp[j-1]种方法
					dp[j]=dp[j]%mod;
					
				}}
			}
		}
		cout<<dp[s2.size()-1]<<endl;//s2的长度为下标的dp值即为出现次数
 	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值