4420: 01串

链接

[http://oj.acm.zstu.edu.cn/JudgeOnline/problem.php?id=4420]

题意

Description
现在我们有一个由01组成的字符串。我们需要在上面做一些操作。每次我们都选一个“01”子串,把他替换成110。如果找不到“01”子串,那么操作结束。请输出总共进行了多少次操作,结果请mod 1e9+7。

Ps:“01”子串必须是相邻的0和1,字符串中只包含0和1。

Input
首先一个T,代表接下来有T行。

接下来的T行,每行一个字符串str

(1<= T <= 200, 所有字符串长度和 len <= 2e7)

Output
对于每个字符串,输出相应操作数。

Sample Input
2
01
001
Sample Output
1
3
HINT
对于第二个字符串:

001 -> 0110 -> 11010 -> 111100

分析

从后面找1的数量,一遇到0,就统计ans,sum=2*sum;
一直到开始即可

代码

#include<iostream>
#include<string.h>
#include<string>
using namespace std;
#define ll long long
const ll mod=1e9+7;
ll solve(string s){
    ll sum=0,ans=0;
    int len=s.length();
    for(int i=len-1;i>=0;i--){
        if(s[i]=='1') {
            sum++; sum%=mod;
        }
        else {
            ans+=sum; ans%=mod;
            sum=2*sum; sum%=mod;
        }
    }
    return ans%mod;
}
int main(){
    int t;
    cin>>t;
    while(t--){
        string s;
        cin>>s;
        cout<<solve(s)<<endl;
    }
    return 0;
}

转载于:https://www.cnblogs.com/mch5201314/p/10084052.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值