C - GeT AC

Time Limit: 2 sec / Memory Limit: 1024 MB

Score :
300
points

Problem Statement

You are given a string Sof length consisting of A, C, G and T. Answer the following Q queries:

Query i (1≤i≤Q): You will be given integers li and ri (1≤li<ri<=N). Consider the substring of S starting at index li and ending at index r i
(both inclusive). In this string, how many times does AC occurs as a substring?
Notes
A substring of a string T is a string obtained by removing zero or more characters from the beginning and the end of T.

Sample Input 1
Copy
8 3
ACACTACG
3 7
2 3
1 8

Sample Output 1
Copy
2
0
3
题目思路:
本题主要的坑是数据量访问的问题,题目很好做,但是如果不进行优化的话铁定超时,所以运用到了一个额外的数组,用来储存

代码:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<iostream>
#include<algorithm>
#include<map>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<string>
#define ll long long
using namespace std;

int main() {
	int n, m;
	while (cin >> n >> m) {
		string s;
		cin >> s;
		int a, b;
		int dp[200005];//用来储存AC出现的=,出现则+1
		memset(dp, 0, sizeof(dp));
		for (int i = 1; i < s.length(); i++) {
			if (s[i] == 'C' && s[i - 1] == 'A') {
				dp[i] = dp[i - 1] + 1;
			}
			else {
				dp[i] = dp[i - 1];
			}
		}
		while (cin >> a >> b) {
			int count;
			count = dp[b - 1] - dp[a - 1];//需要输出哪个区间的个数直接减就可以
			cout << count << endl;
		}
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_44231195/article/details/89276765
ac