【Codeforces】 CF914F Substrings in a String

本文介绍了如何使用bitset数据结构进行字符串匹配,针对长度不超过10^5的查询字符串,提出了一种线性时间复杂度的方法,通过动态调整bitset并处理边界情况。

题目链接

CF方向
Luogu方向

题目解法

又学会了一种字符串匹配 + 骗分技巧!!!

b i t s e t bitset bitset 也可以字符串匹配!!!
考虑询问的字符串的总长度只有 1 0 5 10^5 105,所以可以考虑 n 2 ω \frac{n^2}{\omega} ωn2 的做法
考虑从前往后遍历查询字符串,然后只要把之前匹配的结果往后移一位,然后 & \& & 当前字符的出现位置即可
时间复杂度 O ( n 2 ω ) O(\frac{n^2}{\omega}) O(ωn2)
有些边界情况需要特判

#include <bits/stdc++.h>
using namespace std;
const int N=100100;
char str[N],y[N];
bitset<N> bs[26];
int main(){
	scanf("%s",str+1);
	int n=strlen(str+1);
	for(int i=1;i<=n;i++) bs[str[i]-'a'][i]=1;
	int q;scanf("%d",&q);
	while(q--){
		int op,x;scanf("%d%d",&op,&x);
		if(op==1){
            scanf("%s",y);
			bs[str[x]-'a'][x]=0,bs[y[0]-'a'][x]=1;
			str[x]=y[0];
		}
		else{
			int z;scanf("%d%s",&z,y+1);
			int len=strlen(y+1);
			bitset<N> cur;cur.set();
			for(int i=1;i<=len;i++) cur=(cur<<1)&bs[y[i]-'a'];
            x+=len-1;
            if(x>z){ puts("0");continue;}
			cur>>=x;int ans=cur.count();
			cur>>=(z-x+1);ans-=cur.count();
			printf("%d\n",ans);
		}
	}
	fprintf(stderr,"%d ms\n",int(1e3*clock()/CLOCKS_PER_SEC));
	return 0;
}

### Problem Overview The problem "Having Been a Treasurer in the Past, I Help Goblins Deceive" involves determining the minimum number of operations required to adjust a sum of money to a target value, given constraints on how much each operation can change the total. ### Problem Statement Given `n` (maximum number of operations), `k` (target value), and `p` (amount by which the total can be changed per operation), the goal is to compute the minimum number of operations needed to reach the target `k` using steps of size `p`. If it's impossible to reach the target within the given constraints, return `-1`. ### Solution Approach The solution involves a greedy strategy to determine the minimum number of steps required to reach the target. It also checks whether it's possible to achieve the target within the allowed number of operations. ### Key Observations - If the absolute value of the target `k` is greater than `n * p`, it's impossible to reach the target, and the result should be `-1`. - The number of steps required to reach the target can be calculated as the quotient of `|k| / p`, with an additional step if there's a remainder. - This approach ensures that the solution is efficient and adheres to the constraints of the problem. ### Code Implementation ```cpp #include <bits/stdc++.h> using namespace std; void solve() { int n, k, p; cin >> n >> k >> p; int res = abs(k) / p; if (abs(k) % p) { res += 1; } if (res > n) { cout << "-1" << endl; } else { cout << res << endl; } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int t; cin >> t; while (t--) { solve(); } return 0; } ``` ### Explanation of the Code - The code reads multiple test cases and processes each one individually. - For each test case, it calculates the minimum number of operations required to reach the target `k` using steps of size `p`. - If the number of operations exceeds `n`, it outputs `-1` to indicate that the target is unreachable. - Otherwise, it outputs the computed number of operations[^2]. ### Example Scenarios Consider the following example: - Input: `n = 5`, `k = 12`, `p = 3` - Output: `4` (since `12 / 3 = 4`) Another example: - Input: `n = 3`, `k = 10`, `p = 3` - Output: `-1` (since `10 / 3 = 3.33`, and `4` operations are needed, which exceeds `n = 3`) ### Time and Space Complexity - **Time Complexity**: `O(1)` per test case, as the computation involves simple arithmetic operations. - **Space Complexity**: `O(1)`, as no additional space is used beyond the input variables. ### Conclusion This problem demonstrates the use of greedy algorithms to solve a practical problem in programming competitions. The approach is both efficient and straightforward, making it suitable for time-constrained environments like contests.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值