其他分享
首页 > 其他分享> > Convert the String (模拟)

Convert the String (模拟)

作者:互联网

wh 23:44:24


wh 23:44:34


wh 23:44:59


wh 23:45:04


wh 23:45:12


wh 23:45:18


wh 23:45:24


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include <sstream>
#include<vector>
#include<cmath>    
#include<stack>
#include<time.h>
#include<ctime>
using namespace std;
#define inf 1<<30
#define eps 1e-7
#define LD long double
#define LL long long
#define maxn 100000005        
int ans[1001][1001] = {};
int main()
{
    int T;
    scanf("%d", &T);
    while (T--)
    {
        int N, flag = 0, step = 0;
        scanf("%d", &N);
        string s1, s2;
        int Mina = 'z'-'a', Minb = 'z'-'a';
        cin >> s1 >> s2;
        for (int i = 0; i < N; i++)
        {
            if (s2[i] > s1[i])
            {
                flag = 1;
                break;
            }
            Mina = min(Mina, s1[i] - 'a');
            Minb = min(Minb, s2[i] - 'a');
        }
        
        if (Mina != Minb)
        {
            flag = 1;
        }
        for (int i = 0; i < N; i++)
        {
            int flag2 = 1;
            for (int j = 0; j < N; j++)
            {
                if (s2[i] == s1[j])
                {
                    flag2 = 0;
                    break;
                }
            }
            if (flag2 == 1)
            {
                flag = 1;
                break;
            }
        }
        if (flag == 1)
        {
            printf("-1\n");
            continue;
        }
        while (s1 != s2)
        {
            int flag3 = 0;
            int step2 = 1;
            int Maxc;
            for (int i = 0; i < N; i++)
            {
                if (s1[i] > s2[i])
                {
                    if (flag3 == 0)
                    {
                        Maxc = s2[i]-'a';
                        flag3 = 1;
                    }
                    else
                    {
                        Maxc = max(Maxc, s2[i] - 'a');
                    }
                }
            }
            for (int i = 0; i < N; i++)
            {
                if (s1[i] - 'a' >= Maxc && s1[i] - 'a' >= s2[i] - 'a'&&s2[i]-'a'<=Maxc)
                {
                    ans[step][step2] = i;
                    step2++;
                    s1[i] = Maxc + 'a';
                }
            }
            ans[step][0] = step2 - 1;
            step++;
        }
        printf("%d\n", step);
        for (int i = 0; i < step; i++)
        {
            for (int j = 0; j <= ans[i][0]; j++)
            {
                if (j == 0)
                {
                    printf("%d", ans[i][j]);
                }
                else
                {
                    printf(" %d", ans[i][j]);
                }
            }
            printf("\n");
        }
    }
}

 

标签:Convert,String,23,int,s2,s1,Maxc,include,模拟
来源: https://www.cnblogs.com/whhh/p/13047288.html