Convert the String (模拟)
Posted whhh
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Convert the String (模拟)相关的知识,希望对你有一定的参考价值。
题意:给你两个字符串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 "); 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 ", 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(" "); } } }
以上是关于Convert the String (模拟)的主要内容,如果未能解决你的问题,请参考以下文章
Can't convert the date-like value to string because it isn't known if it's a date (no ti
LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and th
[翻译]Convert a date to the RFC822 standard for use in RSS feeds(在RSS源中间日期转换成RFC822标准使用)
EasyExcel读取文件的问题:Convert excel format exception.You can try specifying the ‘excelType‘ yourself