G - 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 maybe too large, you should output the answer modulo 109+7
.
Input
The input may contain multiple test cases.

Each test case contains exactly two lines. The first line is Clair’s string, and the second line is Bob’s string. The length of both strings are no more than 5000.

Output
For each test case, 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
.
Sample Input

eeettt
et
eeettt
te

Sample Output

9
0
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
ll dp[100005];
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];
                        dp[j]=dp[j]%mod;
                    }
                }
            }
        }
        cout<<dp[s2.size()-1]<<endl;
    }
    
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
修改以下代码:package demo; import java.awt.; import java.awt.event.; import javax.swing.*; public class NumGuess extends Frame{ int k ; Label lbTitle; Label lbGuess; TextField textNum; Button btn_ok; Button btn_cancel; public void fun(){ k = (int)(Math.random()*1000); System.out.println("k="+k); this.setTitle("猜数字游戏"); this.setSize(260,170); this.setLayout(null); textNum = new TextField(); lbTitle = new Label("猜数字游戏"); lbTitle = new Label("请输入你猜的游戏"); btn_ok = new Button("确定"); btn_cancel = new Button("取消");//取消按钮 //设置每个组件在窗口中的摆放位置及长度和宽度 lbTitle.setBounds(100,50,80,20); lbGuess.setBounds(40,80,120,20); textNum.setBounds(170,80,50,20); btn_ok.setBounds(45,120,80,25); btn_cancel.setBounds(135,120,80,25); btn_ok.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ try(string guessNum = textNum;getText ();//要取单行编辑框里的内容int g = Integer,parseInt(guessNum)://将获取到的单行编辑里的内容由字符串转换成整型信 6 if(g>k)( g JoptionPane.showMessageDialog(null,"满大7!"): )else if(g<k)iJOptionPane.showMessagebialog(null,"精小了!") else(JoptionPane,showyessageDialog(null,"恭喜你,满中了,数字为"+g); k = (int)(Math.random()*1000)System.out.println("new k-"+k)*textNum.setText《""); ] catch (NumberFornataxception ex)joptionpane showqepialog(mul1.cextNum,setTert《" } public static void main(String[] args) { // TODO Auto-generated method stub } }
最新发布
05-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值