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;
}

猜你喜欢

转载自blog.csdn.net/weixin_44417851/article/details/89257758
ac
get
今日推荐