C - The Fair Nut and String

C - The Fair Nut and String

 CodeForces - 1084C 

给出一个序列,求符合规则的序列有多少个,规则有两种,一种是只含a,一种是两个a之间夹着b。

对于每一个a有两种选择,一种是取,一种是不取,例如“aaa”,对应4种选择,a,aa,aaa,0/。为什么会有不取的选择,因为如果取这些a,那么下一波遇到一个a,就可以和这些形成规则2,如果不取,那么下一波遇到的a就代表着规则1。所以每次计数器清零后都应该归1,而不是归0;

#include <iostream>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <cstring>
#include <string>
#define ll long long
const int mod = 1e9 + 7;
using namespace std;

int main()
{
	string str;
	ll ans = 0;
	cin >> str;
	int len = str.length();
	ans = 1;
	ll num = 1;
	for (int i = 0; i < len; i++) {
		if (str[i] == 'a') num++;
		else if (str[i] == 'b') {
			ans = ans * num % mod;
			num = 1;
		}
	}
	ans = ans * num % mod;
	ans -= 1;
	cout << ans << endl;

}

猜你喜欢

转载自blog.csdn.net/qq_41539869/article/details/86715709