1. 程式人生 > 實用技巧 >Codeforces #659 C. String Transformation 1

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;
}