1149A. Even Substrings by mrhao61 每个字符转换成数字存在数组里会超时

不知道为什么之前一个一个的转换成数字存在数组里会超时

#include<iostream>
#include<vector>
using namespace std;
int main()
{
	int n;cin>>n;
	int ans=0;
	char a[65003];cin>>a;
	for(int i=0;i<n;i++)
	{
		if((a[i]-'0')%2==0)
		{
			ans++;
			ans=ans+i;
		}
	}
	cout<<ans;
}

A. Even Substrings
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.

猜你喜欢

转载自blog.csdn.net/weixin_43870649/article/details/88843164
今日推荐