Beautiful Sandwich

Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.

Chef is going to participate in 'Who will prepare the most beautiful sandwich?' reality show. The show requires participants to prepare sandwiches that not only look beautiful, but taste delicious too. The show also releases a list of ingredients that you are allowed to use in the sandwich. This time, the list consists of K ingredients. For the purpose of our problem, you can assume that each ingredient is denoted by a lower case Latin character. So the first K lower case Latin letters will describe all the ingredients.

Chef's sandwich consists of N parts of ingredients packed in a row. You can think of it as a string of length N containing the first K English letters only.

Chef thinks the beauty of the sandwich is an integer that can be computed in the following way: 

First partition the sandwich into consecutive blocks of maximal length such that all the parts in the same block are of same ingredient.
Beauty of this sandwich is the sum of squared lengths of the blocks.


For example, let the sandwich be denoted by "aadddbabb".

Chef partitions it in this way - aa, ddd, b, a, bb
Beauty of this sandwich is equal to 2*2 + 3*3 + 1*1 + 1*1 + 2*2 = 19


There will be Q judges in the show. Chef diligently worked for days in preparing Q such sandwiches before the show. Now, it is the show time. He just got an information from the back stage anchor that some of the judges are allergic to some ingredients. Now, Chef has to sit down and take out each sandwich and remove all the ingredients that the judge (the one who is supposed to taste it) is allergic to. After doing that, he will rearrange the parts of the sandwich continuously without leaving any ugly looking gap.


Now, Chef is worrying about the beauty of the sandwiches presented to various judges. He asks you to help in this.

Input
The first line of the input contains two space separated integers N and K, denoting the number of parts in the sandwich and the number of available ingredients, respectively.
The second line contains a string of length N having only the first K Latin lower case letters, denoting the sandwich.
The third line contains a single integer Q, denoting the number of judges.
Each of the following Q lines contains a string denoting the ingredients to which the corresponding judge is allergic. The string will be sorted in ascending order of their characters. No character will appear more than once in it.
Output
For each query, output a single integer in a separate line denoting the beauty of the sandwich that the corresponding judge will be tasting.
Constraints
1 ≤ N ≤ 200,000
1 ≤ K ≤ 18
1 ≤ Q ≤ 262,143
Example
Input:
7 4
acabada
2
b
ac

Output:
8
2
Explanation
First judge is allergic to ingredient denoted by 'b'. So Chef has to remove this ingredient from the judge's sandwich. After that, the sandwich will be "acaada". Its beauty will be 1*1 + 1*1 + 2*2 + 1*1 + 1*1 = 8

Second judge is allergic to ingredients denoted by characters 'a' and 'c'. After removing these ingredients, the sandwich will become "bd" and its beauty will be 2.

Example input:

Example output:


Solution:

    #include <bits/stdc++.h>
    #define ll long long
    #define ull unsigned long long
    #define pii pair<int,int>
    #define pil pair<int,long long>
    #define pli pair<long long,int>
    #define pll pair<long long,long long>
    #define pb push_back
    #define mk make_pair
    #define eps 1e-12
    #define maxn 200009
    using namespace std;
    ll dp[1<<18][18],sq[18],cnt[18][maxn];
    vector<int> vv[18];
    vector<pil> v;
    int main()
    {
    	ios_base::sync_with_stdio(false);
    	cin.tie(0);
    	cout.tie(0);
    	int n,k;
    	cin>>n>>k;
    	string s;
    	cin>>s;
    	ll cnt1=1;
    	for(int i=0;i<s.size()-1;i++)
    	{
    		if(s[i]!=s[i+1])
    		{
    			v.pb(mk(s[i]-'a',cnt1));
    			cnt1=1;
    		}
    		else
    		{
    			cnt1++;
    		}
    	}
    	v.pb(mk(s[s.size()-1]-'a',cnt1));
    	for(int i=0;i<v.size();i++)
    	{
    		sq[v[i].first]+=(v[i].second*v[i].second);
    	}
    	for(int i=0;i<s.size();i++)
    	{
    		cnt[s[i]-'a'][i]++;
    		for(int j=0;j<k;j++)
    		{
    			cnt[j][i]+=cnt[j][i-1];
    		}
    		vv[s[i]-'a'].pb(i);
    	}
    	for(int i=0;i<s.size();i++)
    	{
    		vector<pii> occ;
    		for(int j=0;j<k;j++)
    		{
    			auto it=lower_bound(vv[j].begin(),vv[j].end(),i+1);
    			if(it!=vv[j].begin())
    			{
    				it--;
    				occ.pb(mk((*it),j));
    			}
    		}
    		sort(occ.begin(),occ.end(),greater<pii>());
    		int mask=0;
    		for(int j=1;j<occ.size()-1;j++)
    		{
    			mask=mask|(1<<(occ[j].second));
    			dp[mask][s[i]-'a']+=2*(cnt[s[i]-'a'][occ[j].first]-cnt[s[i]-'a'][occ[j+1].first]);
    		}
    		mask=mask|(1<<occ[occ.size()-1].second);
    		if(occ.size()>=2)
    			dp[mask][s[i]-'a']+=2*(cnt[s[i]-'a'][occ[occ.size()-1].first]-cnt[s[i]-'a'][0]+((ll)(s[0]==s[i])));
    	}
    	for(int j=0;j<k;j++)
    	{
    		for(int i=0;i<k;i++)
    		{
    			for(int mask=0;mask<(1<<k);mask++)
    			{
    				if(mask&(1<<i))
    				{
    					dp[mask][j]+=dp[mask^(1<<i)][j];
    				}
    			}
    		}
    	}
    	for(int mask=0;mask<(1<<k);mask++)
    	{
    		for(int j=0;j<k;j++)
    		{
    			dp[mask][j]+=sq[j];
    		}
    	}
    	int q;
    	cin>>q;
    	while(q--)
    	{
    		ll ans=0;
    		string w;
    		cin>>w;
    		int mask=0;
    		for(int i=0;i<w.size();i++)
    		{
    			mask=mask|(1<<(w[i]-'a'));
    		}
    		for(int i=0;i<k;i++)
    		{
    			if(!(mask&(1<<i)))
    			{
    				ans=ans+dp[mask][i];
    			}
    		}
    		cout<<ans<<"\n";
    	}
    }