You are given a string s that contains digits 0-9, addition symbols '+', and multiplication symbols '*'only , representing a valid math expression of single digit numbers (e.g., 3+5*2). This expression was given to n elementary school students. The students were instructed to get the answer of the expression by following this order of operations :
Compute multiplication , reading from left to right ; Then,
Compute addition , reading from left to right.
You are given an integer array answers of length n, which are the submitted answers of the students in no particular order. You are asked to grade the answers, by following these rules :
If an answer equals the correct answer of the expression, this student will be rewarded 5 points;
Otherwise, if the answer could be interpreted as if the student applied the operators in the wrong order but had correct arithmetic , this student will be rewarded 2 points;
Otherwise, this student will be rewarded 0 points.

Input: s ="7+3*1*2", answers =[20,13,42]Output: 7Explanation: As illustrated above, the correct answer of the expression is13, therefore one student is rewarded 5 points:[20,_**13**_ ,42]A student might have applied the operators inthis wrong order:((7+3)*1)*2=20. Therefore one student is rewarded 2 points:[_**20**_ ,13,42]The points for the students are:[2,5,0]. The sum of the points is2+5+0=7.
Input: s ="3+5*2", answers =[13,0,10,13,13,16,16]Output: 19Explanation: The correct answer of the expression is13, therefore three students are rewarded 5 points each:[**_13_**,0,10,**_13_**,**_13_**,16,16]A student might have applied the operators inthis wrong order:((3+5)*2=16. Therefore two students are rewarded 2 points:[13,0,10,13,13,**_16_**,**_16_**]The points for the students are:[5,0,0,5,5,2,2]. The sum of the points is5+0+0+5+5+2+2=19.
Input: s ="6+0*1", answers =[12,9,6,4,8,6]Output: 10Explanation: The correct answer of the expression is6.If a student had incorrectly done(6+0)*1, the answer would also be 6.By the rules of grading, the students will still be rewarded 5points(as they got the correct answer), not 2 points.The points for the students are:[0,0,5,0,0,5]. The sum of the points is10.
Method 1 - DP for All Possible Results, Stack for Correct Answer#
We use dynamic programming to compute all possible results for each subexpression (with any parenthesization), and a stack to compute the correct answer (left-to-right precedence). For each answer, award 5 points if correct, 2 if in the set of possible wrong results, 0 otherwise.
#include<vector>#include<string>#include<unordered_set>usingnamespace std;
classSolution {
public:int scoreOfStudents(string s, vector<int>& answers) {
int n = (s.size() +1) /2;
vector<vector<unordered_set<int>>> dp(n, vector<unordered_set<int>>(n));
for (int i =0; i < n; ++i) dp[i][i].insert(s[2*i] -'0');
for (int len =2; len <= n; ++len) {
for (int i =0; i + len -1< n; ++i) {
int j = i + len -1;
for (int k = i; k < j; ++k) {
for (int a : dp[i][k]) for (int b : dp[k+1][j]) {
char op = s[2*k+1];
int val = (op =='+') ? a + b : a * b;
if (val <=1000) dp[i][j].insert(val);
}
}
}
}
int correct = eval(s);
int res =0;
for (int x : answers) {
if (x == correct) res +=5;
elseif (dp[0][n-1].count(x)) res +=2;
}
return res;
}
inteval(const string& s) {
vector<int> nums;
char op =0;
for (char c : s) {
if (isdigit(c)) {
int d = c -'0';
if (op =='*') {
nums.back() *= d;
op =0;
} else {
nums.push_back(d);
}
} elseif (c =='+'|| c =='*') {
op = c;
}
}
int sum =0;
for (int x : nums) sum += x;
return sum;
}
};
import java.util.*;
classSolution {
publicintscoreOfStudents(String s, int[] answers) {
int n = (s.length() + 1) / 2;
Set<Integer>[][] dp =new HashSet[n][n];
for (int i = 0; i < n; ++i) {
dp[i][i]=new HashSet<>();
dp[i][i].add(s.charAt(2*i) -'0');
}
for (int len = 2; len <= n; ++len) {
for (int i = 0; i + len - 1 < n; ++i) {
int j = i + len - 1;
dp[i][j]=new HashSet<>();
for (int k = i; k < j; ++k) {
for (int a : dp[i][k]) for (int b : dp[k+1][j]) {
char op = s.charAt(2*k+1);
int val = op =='+'? a + b : a * b;
if (val <= 1000) dp[i][j].add(val);
}
}
}
}
int correct = eval(s);
int res = 0;
for (int x : answers) {
if (x == correct) res += 5;
elseif (dp[0][n-1].contains(x)) res += 2;
}
return res;
}
privateinteval(String s) {
List<Integer> nums =new ArrayList<>();
char op = 0;
for (char c : s.toCharArray()) {
if (Character.isDigit(c)) {
int d = c -'0';
if (op =='*') {
nums.set(nums.size()-1, nums.get(nums.size()-1) * d);
op = 0;
} else {
nums.add(d);
}
} elseif (c =='+'|| c =='*') {
op = c;
}
}
int sum = 0;
for (int x : nums) sum += x;
return sum;
}
}
from typing import List
classSolution:
defscoreOfStudents(self, s: str, answers: List[int]) -> int:
n = (len(s) +1) //2 dp = [[set() for _ in range(n)] for _ in range(n)]
for i in range(n):
dp[i][i].add(int(s[2*i]))
for l in range(2, n+1):
for i in range(n-l+1):
j = i + l -1for k in range(i, j):
for a in dp[i][k]:
for b in dp[k+1][j]:
op = s[2*k+1]
val = a + b if op =='+'else a * b
if val <=1000:
dp[i][j].add(val)
defeval_expr(s):
nums = []
op =Nonefor c in s:
if c.isdigit():
d = int(c)
if op =='*':
nums[-1] *= d
op =Noneelse:
nums.append(d)
elif c in'+*':
op = c
return sum(nums)
correct = eval_expr(s)
res =0for x in answers:
if x == correct:
res +=5elif x in dp[0][n-1]:
res +=2return res
use std::collections::HashSet;
impl Solution {
pubfnscore_of_students(s: String, answers: Vec<i32>) -> i32 {
let n = (s.len() +1) /2;
let s = s.as_bytes();
letmut dp =vec![vec![HashSet::new(); n]; n];
for i in0..n {
dp[i][i].insert((s[2*i] -b'0') asi32);
}
for len in2..=n {
for i in0..=n-len {
let j = i + len -1;
for k in i..j {
for&a in&dp[i][k] {
for&b in&dp[k+1][j] {
let op = s[2*k+1] aschar;
let val =if op =='+' { a + b } else { a * b };
if val <=1000 {
dp[i][j].insert(val);
}
}
}
}
}
}
fneval(s: &[u8]) -> i32 {
letmut nums =vec![];
letmut op =0u8;
for&c in s {
if c.is_ascii_digit() {
let d = c -b'0';
if op ==b'*' {
let last = nums.pop().unwrap();
nums.push(last * d asi32);
op =0;
} else {
nums.push(d asi32);
}
} elseif c ==b'+'|| c ==b'*' {
op = c;
}
}
nums.iter().sum()
}
let correct = eval(s);
letmut res =0;
for&x in&answers {
if x == correct {
res +=5;
} elseif dp[0][n-1].contains(&x) {
res +=2;
}
}
res
}
}