zoukankan      html  css  js  c++  java
  • String Transformation 1

    String Transformation 1

    题目链接:Codeforces Round #659(div2) C.String Transformation 1

    题目描述

    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:

    1. 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).
    2. 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).
    3. 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

    input

    5
    3
    aab
    bcc
    4
    cabc
    abcb
    3
    abc
    tsr
    4
    aabd
    cccd
    5
    abcbd
    bcdda
    

    output

    2
    -1
    3
    2
    -1
    

    题目大意

    修改字符串A若干次,使A=B。在一次操作中,A中相同的字符x可以同时修改为字符y,y>x。求最少操作次数。

    解题思路

    对于A中的每个字符x,在B中都有对应的字符y,我们的目标是把x变成y。若x=y,则不用修改。对于需要修改的字符,因为字符只能增大,所以我们从a开始修改。对所有需要修改的某个字符x,我们都将它改为最小的y,设为y0,这样没有匹配的将在修改y0时再次修改,直到匹配,不需要多余的操作。

    Code

    #include<cstdio>
    #include<algorithm>
    #include<vector>
    using namespace std;
    
    #define MAXN 100005
    
    char a[MAXN], b[MAXN];
    vector<char> v[22];
    
    int main(void)
    {
        int T; scanf("%d",&T);
        while(T--){
            for(int i=0;i<20;++i) v[i].clear();
            int n; scanf("%d",&n);
            scanf("%s%s",a,b);
            bool flag = true;
            for(int i=0;i<n;++i){
                if(a[i]>b[i]){
                    printf("-1
    ");
                    flag = false;
                    break;
                }
                if(a[i]==b[i]) continue;
                int k = a[i]-'a';
                v[k].push_back(b[i]);
            }
            if(!flag) continue;
            int cnt = 0;
            for(int i=0;i<20;++i){
                sort(v[i].begin(),v[i].end());
                int num = v[i].size();
                if(num==0) continue;
                char y = v[i][0]; int k = y-'a';
                for(int j=0;j<num;++j){
                    if(v[i][j]!=y) v[k].push_back(v[i][j]);
                }
                ++cnt;
            }
            printf("%d
    ",cnt);
        }
        return 0;
    }
    
    海到无边天作岸,山登绝顶我为峰
  • 相关阅读:
    flume 使用遇到问题及解决
    定时任务 Linux cron job 初步使用
    java instrumentation &JVMTI
    Java远程执行Shell命令
    No input clusters found in output/ZS_TEST_OUTPUT3404268420/clusters-0/part-randomSeed. Check your -c argument.
    asp.net core 中读取post 方式来的内容
    C#程序 权限不够的解决方案
    wamp下安装https 实现 ssl 协议,主要是编写小程序通讯
    如何让thinkpad X1C 用U盘 安装上专业版win10
    php 5.4 5.5 如何连接 ms sqlserver
  • 原文地址:https://www.cnblogs.com/Fiona0726/p/13375811.html
Copyright © 2011-2022 走看看