Under construction

Still being built. Things may change

Fox Noodles

Time limit0.5sMemory limit1024 MB

Summary
Given the predicted and actual customer orders, find the sum of every bowl number n that lets the first customer win while a stack-based display case serves all later customers.
Level

Medium7 of 10

Topics
Stack, String, Greedy
Solved
No attempts yet

Problem

Deep in Gwanaksan there is a village where foxes and raccoon dogs live. These animals are good at shapeshifting, so they often turn into humans and visit a nearby university. One day, on the road from the village down to the school, a master fox opened a noodle shop. The shop's signature dishes are fox noodles, topped with fried tofu pouches, and raccoon noodles, topped with tempura crumbs. The shop soon became very popular with the foxes and raccoon dogs of the village. Every fox that visits always orders fox noodles, and every raccoon dog always orders raccoon noodles.

The master fox can predict almost exactly how many foxes and raccoon dogs will visit on a given day from years of experience. He is also practicing predicting the order in which the customers arrive, but he is still quite bad at it. Even so, every morning he predicts the customer order and cooks noodles strictly in that order. Each time he cooks a bowl, he numbers it in sequence starting from 1, so that each bowl can be identified by when it was made.

If the predicted order is wrong and he hands out noodles as soon as they are cooked, some customers will get noodles different from what they ordered. To prevent this, the master fox set up a magic display case that keeps food at its flavor and temperature for a long time. When he cooks a bowl, he places it on the display case right away. When he serves a customer, he always takes out the bowl on the display case that was cooked last, meaning the one with the largest number. At each moment, the master fox can freely choose whether to cook a new bowl or serve a customer.

The shop always has a regular customer who arrives first. The master fox has prepared a small lucky number event for this customer. If the first customer calls a number nn, the master fox must serve the nnth bowl to this customer. If the nnth bowl is not the menu item the first customer ordered, or if no matter how hard the master fox tries, he cannot satisfy the orders of all the customers who come after, the customer receives a miss. Otherwise, the customer receives a win, and the first customer receives heartfelt congratulations.

Given the order the master fox predicted and the order in which the customers actually arrived, find all the lucky numbers that can win.

Input

The first line contains the total number of customers NN who visited in one day (1≤N≤50000001 \leq N \leq 5000000).

The second line contains a string of NN characters showing the customer order predicted by the master fox. The string consists only of the letters Y and N, where Y means fox and N means raccoon dog.

The third line contains a string of NN characters in the same format as the second line, showing the order in which the customers actually arrived.

The two strings have the same number of Y characters, and therefore also the same number of N characters.

Output

On the first line, print the sum of all lucky numbers the first customer can call that result in a win. If every number leads to a miss, print 0.

Hint

In the first example, if the first customer calls 1, the first customer is a raccoon dog but receives fox noodles, so it is a miss.

If the first customer calls 2, the master fox can serve all orders in the way shown below, so it is a win.

In the same way, 3 is also a winning number, so the answer is 2+3=52+3=5.

In the second example, if the first customer calls 4, the menu is correct. However, the master fox then has to serve the remaining bowls on the display case in the order raccoon noodles, fox noodles, raccoon noodles, so he cannot satisfy the orders of the remaining customers.

Examples2

  1. Example 1

    Input
    3
    YNN
    NNY
    
    Expected output
    5
    
  2. Example 2

    Input
    4
    NYNY
    YNNY
    
    Expected output
    2