标签:ble span clu int tin scanf printf include 最大
题意:给你两个字符串s1,s2;你需要用最小变化次数把是s1变成s2;
变化规则如下:
在s1中任意选择k个字符,选择k个字符中字典序最小的si,让k个字符都变成si;
#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])//如果s2比s1的字典序大,很显然是s1变不成的s2,s1只可能比原来小,不可能比原来大 { flag = 1;//记上标记 break; } Mina = min(Mina, s1[i] - ‘a‘);//记录s1中字典序最小的字符 Minb = min(Minb, s2[i] - ‘a‘);//记录s2中字典序最小的字符 } 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])//判断s2中的字符,s1是否都拥有,如果没有,s1也不可能变成s2 { 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])//遍历s1,s2字符串,寻找s1大于s2的对应字符的s2字典序最大的,你不可能跳过最大的反而去变化比它小的 { 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)//s1符合这三个条件 { ans[step][step2] = i;//记录下s1的位置 step2++; s1[i] = Maxc + ‘a‘;//把s1对应位置变成之前找到最大的s2字符 } } ans[step][0] = step2 - 1;//有k个位置 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"); } } }
标签:ble span clu int tin scanf printf include 最大
原文地址:https://www.cnblogs.com/whhh/p/13047288.html