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

1、题目描述

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

代码:

#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;
}

 

上一篇:强连通分量(Tarjan算法) 图解


下一篇:细胞(深度优先遍历)