其他分享
首页 > 其他分享> > 【LeetCode - 1055】形成字符串的最短路径

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

作者:互联网

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

 

标签:cnt,1055,int,++,source,LETTER,字符串,LeetCode,target
来源: https://www.cnblogs.com/gcter/p/15952003.html