GOOD PROBLEM
Analysis
- Directly use DFS of course ETL
- Use DP also ETL
- f[i][j]=f[i-1][j-1](a[i]==b[j] || b[j]=='?')
- or
- f[i][j]=f[k][j-1](i<=k<=a.size())(b[j]=='*')
- PRUNING ONE: Knowing that if there are multiple consecutive * in the string b, then eliminate them and remain one * is ok. This pruning passes all the testcases except for the last one.
- PRUNING TWO: Another pruning comes up with this post. Here let me show the details about this.
Each node(p,q) will have one child if b[q]=='?' or a[p]==b[q], or multi children if b[q]=='*'. ( In the figure we can see that node(p,q) has multi children while node(p,q+1) has one child).
The subtle trick is if we reached the node say (p+2+2,q+1+2) then there is no need to search upper layer's nodes which circled by a dotted line. This can be proved by contradiction in following figure.
- PRUNING THREE. after two pruning above the program still is blocked at the last test case. So the last pruning comes that let aux(k) denote the number of alphabet in b[k..end]. If a search node(p,q) which a.size() - p < aux(q) then we know there won't be a solution from this point.
nice
ReplyDelete