【LeetCode - 1055】形成字符串的最短路径

2023-03-10,,

1、题目描述

代码:

#include <iostream>
#include <string>
using namespace std;
const int MAX_LETTER = 26;
int main()
{
string source;
string target;
cin>>source;
cin>>target;
int cnt[MAX_LETTER] = {0};
for (int i = 0; target[i] != '\0'; i++) {
cnt[target[i] - 'a'] = 1;
}
for (int i = 0; source[i] != '\0'; i++) {
cnt[source[i] - 'a'] = 0;
}
for (int i = 0; i < MAX_LETTER;i++) {
if (cnt[i] == 1) {
cout<< "-1" <<endl;
return 0;
}
}
int t = 0;
int i = 0;
int j = 0;
while (target[i]) {
t++;
j = 0;
while (target[i] && source[j]) {
if (target[i] == source[j]) {
i++;
j++;
} else {
j++;
}
}
}
cout<<t<<endl;
return 0;
}

【LeetCode - 1055】形成字符串最短路径的相关教程结束。

《【LeetCode - 1055】形成字符串的最短路径.doc》

下载本文的Word格式文档,以方便收藏与打印。