Codeforces #659 C. String Transformation 1

题面

Note that the only difference between String Transformation 1 and String Transformation 2 is in the move Koa does. In this version the letter y Koa selects must be strictly greater alphabetically than x (read statement for better understanding). You can make hacks in these problems independently.

Koa the Koala has two strings A and B of the same length n (|A|=|B|=n) consisting of the first 20 lowercase English alphabet letters (ie. from a to t).

In one move Koa:

selects some subset of positions p1,p2,…,pk (k≥1;1≤pi≤n;pi≠pj if i≠j) of A such that Ap1=Ap2=…=Apk=x (ie. all letters on this positions are equal to some letter x).
selects a letter y (from the first 20 lowercase letters in English alphabet) such that y>x (ie. letter y is strictly greater alphabetically than x).
sets each letter in positions p1,p2,…,pk to letter y. More formally: for each i (1≤i≤k) Koa sets Api=y.
Note that you can only modify letters in string A.

Koa wants to know the smallest number of moves she has to do to make strings equal to each other (A=B) or to determine that there is no way to make them equal. Help her!

Input
Each test contains multiple test cases. The first line contains t (1≤t≤10) — the number of test cases. Description of the test cases follows.

The first line of each test case contains one integer n (1≤n≤105) — the length of strings A and B.

The second line of each test case contains string A (|A|=n).

The third line of each test case contains string B (|B|=n).

Both strings consists of the first 20 lowercase English alphabet letters (ie. from a to t).

It is guaranteed that the sum of n over all test cases does not exceed 105.

Output
For each test case:

Print on a single line the smallest number of moves she has to do to make strings equal to each other (A=B) or −1 if there is no way to make them equal.

Example
inputCopy
5
3
aab
bcc
4
cabc
abcb
3
abc
tsr
4
aabd
cccd
5
abcbd
bcdda
outputCopy
2
-1
3
2
-1

思路

可以考虑嗯搜,脑子有点乱,写不出来。直接模拟了,首先,我们要确定一个字串下降的顺序,首先呢,这个肯定是由小字符下降到大字符,所以我们把这个情况放到一个类似于桶的数组里面,哦,对,如果字符串a里面有比b大的那么不存在。接来去的话,按顺序遍历这个桶数组,如果有元素,那么操作数+1,同时我们思考一下,如果这个操作完了之后,我们实际的情况要把所有的该字符都做了改变,那么这个时候我们同样需要修改一下桶的情况。最后的操作数就是答案。

代码实现

#include<cstdio>
#include<iostream>
#include<cstring>
#include<map>
#include<cmath>
using namespace std;
const int maxn=25;

int main ( ) {
   int t;
   cin>>t;
   while (t--) {
       int n;
       string a,b;
       cin>>n>>a>>b;
       int flag=0;
       int t[maxn][maxn];
       memset (t,0,sizeof (t));
       for (int i=0;i<n;i++) {
           if (a[i]>b[i]) {
               flag=1;
               break;
          }
       }
       if (flag) {
           cout<<-1<<endl;
           continue;
       }
       for (int i=0;i<n;i++) {
           if (a[i]!=b[i]) {
             t[a[i]-'a'][b[i]-'a']++;            
           }
       }
       int ans=0;
       for (int i=0;i<24;i++) {
           int index=-1;
           for (int j=i+1;j<24;j++) {
               if (t[i][j]) {
                   ans++;
                   index=j;
                   break;
               }
           }
           if (index==-1) continue;
           for (int j=0;j<24;j++) {
               t[index][j]+=t[i][j];
           }
       }
       cout<<ans<<endl;
   }
   

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/hhlya/p/13376589.html