A. Even Substrings

本文介绍了一种高效算法,用于解决给定只包含1到9数字的字符串中,找出所有表示偶数的子串数量的问题。算法通过遍历字符串并累计偶数前的元素个数来快速得出答案。

time limit per test0.5 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a string s=s1s2…sn of length n, which only contains digits 1, 2, …, 9.

A substring s[l…r] of s is a string slsl+1sl+2…sr. A substring s[l…r] of s is called even if the number represented by it is even.

Find the number of even substrings of s. Note, that even if some substrings are equal as strings, but have different l and r, they are counted as different substrings.

Input
The first line contains an integer n (1≤n≤65000) — the length of the string s.

The second line contains a string s of length n. The string s consists only of digits 1, 2, …, 9.

Output
Print the number of even substrings of s.

Examples
inputCopy
4
1234
outputCopy
6
inputCopy
4
2244
outputCopy
10
Note
In the first example, the [l,r] pairs corresponding to even substrings are:

s[1…2]
s[2…2]
s[1…4]
s[2…4]
s[3…4]
s[4…4]
In the second example, all 10 substrings of s are even substrings. Note, that while substrings s[1…1] and s[2…2] both define the substring “2”, they are still counted as different substrings.

题解:看note之后,发现只要是偶数然后偶数前面的个数累加就出来了

AC代码:

#include <iostream>   
#include <bitset>	  
#include <algorithm>   
#include <vector>
#include <map>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <queue> 
using namespace std;
int main()
{
	int n,sum=0;
	char a[70000];
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
		if((a[i]-'0')%2==0)
		{
			sum+=i+1;
		}
	}
	cout<<sum<<endl;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值