C. The Number Of Good Substrings(思维&位运算)

本文介绍了一种通过位运算和暴力遍历的方法来计算特定条件下好串的数量。对于给定的01串,若一个子串的长度等于其二进制数值,则称其为好串。文章提供了完整的C++代码实现,并解释了如何快速有效地完成这一计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

题意:给出一串01串,我们要计算其好串的数量。对于sl,sl+1,…,sr 如果r−l+1=f(sl…sr).则其为好串,f(sl…sr)=从sl开始的二进制数值.

思路:直接暴力+位运算,观察一下就能发现我们要求的字符串长度只有21e5,而一个二进制数当其长度大于20的话已经大于了21e5,所以我们只需全部遍历一遍,每当发现一个1后向后遍历20位即可。而对于每一次是否能形成一个好串只需判断这个位置的前导零的个数+这个数往后的长度是否大于从这个数往后遍历一定长度由二进制转换成十进制的值,模拟一下二进制转换成十进制即可。

#include<iostream>
#include<string>
#include<map>
#include<algorithm>
#include<memory.h>
#include<cmath>
#define pii pair<int,int>
#define FAST ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
typedef long long ll;
const int Max = 1e6 + 5;
int lst[Max];
int Mod = 1e9 + 7;
int sum[Max];
int main()
{
	FAST;
	int t;cin >> t;
	while (t--)
	{
		string str;cin >> str;
		str.insert(str.begin(), '-');
		int n = str.size() - 1, s = 0;
		for (int i = 1;i <= n;i++)
		{
			if (str[i] == '0')s++;
			else
			{
				sum[i] = s;
				s = 0;
			}
		}
		int ans = 0;
		for (int i = 1;i <= n;i++) {
			if (str[i] == '0')continue;
			ll ss = 0;
			for (int j = i;j <= min(i + 20, n);j++)
			{
				ss *= 2;
				if (str[j] == '1')
					ss++;
				if (ss <= sum[i] + 1+j-i)ans++;
			}
		}
		cout << ans << endl;
	}
}

D. Good Substrings time limit per test2 seconds memory limit per test512 megabytes You&#39;ve got string s, consisting of small English letters. Some of the English letters are good, the rest are bad. A substring s[l...r] (1&thinsp;&le;&thinsp;l&thinsp;&le;&thinsp;r&thinsp;&le;&thinsp;|s|) of string s&thinsp;&thinsp;=&thinsp;&thinsp;s1s2...s|s| (where |s| is the length of string s) is string &thinsp;slsl&thinsp;+&thinsp;1...sr. The substring s[l...r] is good, if among the letters &thinsp;sl,&thinsp;sl&thinsp;+&thinsp;1,&thinsp;...,&thinsp;sr there are at most k bad ones (look at the sample&#39;s explanation to understand it more clear). Your task is to find the number of distinct good substrings of the given string s. Two substrings s[x...y] and s[p...q] are considered distinct if their content is different, i.e. s[x...y]&thinsp;&ne;&thinsp;s[p...q]. Input The first line of the input is the non-empty string s, consisting of small English letters, the string&#39;s length is at most 1500 characters. The second line of the input is the string of characters &quot;0&quot; and &quot;1&quot;, the length is exactly 26 characters. If the i-th character of this string equals &quot;1&quot;, then the i-th English letter is good, otherwise it&#39;s bad. That is, the first character of this string corresponds to letter &quot;a&quot;, the second one corresponds to letter &quot;b&quot; and so on. The third line of the input consists a single integer k (0&thinsp;&le;&thinsp;k&thinsp;&le;&thinsp;|s|) &mdash; the maximum acceptable number of bad characters in a good substring. Output Print a single integer &mdash; the number of distinct good substrings of string s. Examples InputCopy ababab 01000000000000000000000000 1 OutputCopy 5 InputCopy acbacbacaa 00000000000000000000000000 2 OutputCopy 8 Note In the first example there are following good substrings: &quot;a&quot;, &quot;ab&quot;, &quot;b&quot;, &quot;ba&quot;, &quot;bab&quot;. In the second example there are following good substrings: &quot;a&quot;, &quot;aa&quot;, &quot;ac&quot;, &quot;b&quot;, &quot;ba&quot;, &quot;c&quot;, &quot;ca&quot;, &quot;cb&quot;.
最新发布
03-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_Rikka_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值