GeT AC

在这里插入图片描述

Output

Print Q lines. The i-th line should contain the answer to the i-th query.

Sample Input 1

8 3
ACACTACG
3 7
2 3
1 8

Sample Output 1

2
0
3

Query 1: the substring of S starting at index 3 and ending at index 7 is ACTAC. In this string, AC occurs twice as a substring.
Query 2: the substring of S starting at index 2 and ending at index 3 is CA. In this string, AC occurs zero times as a substring.
Query 3: the substring of S starting at index 1 and ending at index 8 is ACACTACG. In this string, AC occurs three times as a substring.

思路:

将一个做这道题的主思路,举个例子吧,例子容易理解:a[1]=1;a[2]=1+2:a[3]=1+2+3……a[n]=1+2+3+……+n;那么求第四个到底二十个的和怎么求?先明白一个简单的例子:从2到6之间的和是a[6]-a[2]=1+2+3+4+5+6-(1+2)+2=3+4+5+6+2,这个例子看懂的话,那么上一个例子是不是就明白了,a[20]-a[4]+4就是从20到4之间的和。那么意明白,接下来这道题就好做了。

代码:

#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
int main() {
    long long n,m;string s;
    while(cin>>n>>m>>s){
    	int a[200000],v=0;memset(a,0,sizeof(a));
    	for(int i=0;i<s.length();i++){
    		a[i]=v;
    		if(s[i]=='A'&&s[i+1]=='C'){
    			a[i+1]=v+1;i++;v++;
			}
		}
		cout<<endl;
		for(int i=0;i<m;i++){
			int x,y;cin>>x>>y;
			cout<<a[y-1]-a[x-1]<<endl;
		}
	}
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

GUESSERR

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

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

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

打赏作者

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

抵扣说明:

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

余额充值