Problem:
Score : 500 points

Problem Statement

Given are two strings s and t consisting of lowercase English letters. Determine if there exists an integer i satisfying the following condition, and find the minimum such i if it exists.

Let s' be the concatenation of 10^{100} copies of s. t is a subsequence of the string {s'}_1{s'}_2\ldots{s'}_i (the first i characters in s').

Notes

A subsequence of a string a is a string obtained by deleting zero or more characters from a and concatenating the remaining characters without changing the relative order. For example, the subsequences of contest include net, c, and contest.

Constraints

1 \leq |s| \leq 10^5

1 \leq |t| \leq 10^5

s and t consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s
t

Output

If there exists an integer i satisfying the following condition, print the minimum such i; otherwise, print -1.

Sample Input 1

contest
son

Sample Output 1

10

t = son is a subsequence of the string contestcon (the first 10 characters in s' = contestcontestcontest...), so i = 10 satisfies the condition.

On the other hand, t is not a subsequence of the string contestco (the first 9 characters in s'), so i = 9 does not satisfy the condition.

Similarly, any integer less than 9 does not satisfy the condition, either. Thus, the minimum integer i satisfying the condition is 10.

Sample Input 2

contest
programming

Sample Output 2

-1

t = programming is not a substring of s' = contestcontestcontest.... Thus, there is no integer i satisfying the condition.

Sample Input 3

contest
sentence

Sample Output 3

33

Note that the answer may not fit into a 32-bit integer type, though we cannot put such a case here.

Solution in C (GCC 5.4.1) (judge verdict: Accepted)

#include <float.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

// 内部定数
#define D_ARRAY_MAX		100005									// 最大配列数
#define D_CHAR_CNT		26										// 文字種類数

// 内部変数
static FILE *szpFpI;											// 入力
static char sc1Str[D_ARRAY_MAX];								// 文字列
static int si2Pos[D_CHAR_CNT][D_ARRAY_MAX];						// 位置
static int si1PCnt[D_CHAR_CNT];									// 位置数

// 内部変数 - テスト用
#ifdef D_TEST
	static int siRes;
	static FILE *szpFpA;
	static int siTNo;
#endif

// 出力
int
fOut(
	char *pcpLine				// <I> １行
)
{
	char lc1Buf[1024];

#ifdef D_TEST
	fgets(lc1Buf, sizeof(lc1Buf), szpFpA);
	if (strcmp(lc1Buf, pcpLine)) {
		siRes = -1;
	}
#else
	printf("%s", pcpLine);
#endif

	return 0;
}

// 検索
// 戻り値：[>=0]配列番号 [-1]なし
int
fBSrhPN(
	int piVal					// <I> 値
	, int *pipArray				// <I> 配列
	, int piACnt				// <I> 配列数
)
{
	// 初期範囲
	int liSNo = 0;
	int liENo = piACnt - 1;

	// 検索
	while (1) {

		// 中間位置
		int liMNo = (liSNo + liENo) / 2;

		// 一致チェック
		if (piVal == pipArray[liMNo]) {
			return liMNo;
		}

		// 範囲を絞る
		if (piVal < pipArray[liMNo]) {				// 左側へ
			if (liSNo < liMNo) {						// 範囲あり
				liENo = liMNo - 1;
			}
			else {										// 範囲なし
				return liMNo;
			}
		}
		else {										// 右側へ
			if (liENo > liMNo) {						// 範囲あり
				liSNo = liMNo + 1;
			}
			else {										// 範囲なし
				return liMNo + 1;
			}
		}
	}

	return -1;
}

// 実行メイン
long long
fMain(
)
{
	int i;

	// 文字列s - 取得
	fgets(sc1Str, sizeof(sc1Str), szpFpI);

	// 位置 - 取得
	int liCNo, liLen;
	for (i = 0; ; i++) {
		liCNo = sc1Str[i] - 'a';
		if (liCNo < 0 || D_CHAR_CNT <= liCNo) {
			break;
		}

		// 位置 - セット
		si2Pos[liCNo][si1PCnt[liCNo]] = i;
		si1PCnt[liCNo]++;
	}
	liLen = i;

	// 文字列t - 取得
	fgets(sc1Str, sizeof(sc1Str), szpFpI);

	// 文字数 - 取得
	int liRep = 0;
	int liNow = -1;
	for (i = 0; ; i++) {
		liCNo = sc1Str[i] - 'a';
		if (liCNo < 0 || D_CHAR_CNT <= liCNo) {
			break;
		}

		// 文字有無
		if (si1PCnt[liCNo] < 1) {
			return -1;
		}

		// 文字検索
		int liPos;
		liPos = fBSrhPN(liNow + 1, si2Pos[liCNo], si1PCnt[liCNo]);
		if (liPos < si1PCnt[liCNo]) {
			liNow = si2Pos[liCNo][liPos];
		}
		else {
			liRep++;
			liNow = si2Pos[liCNo][0];
		}
	}

	return (long long)liLen * (long long)liRep + (long long)liNow + 1;
}

// １回実行
int
fOne(
)
{
	long long llRet;
	char lc1Buf[1024];

	// データ - 初期化
	memset(si1PCnt, 0, sizeof(si1PCnt));						// 位置数

	// 入力 - セット
#ifdef D_TEST
	sprintf(lc1Buf, ".\\Test\\T%d.txt", siTNo);
	szpFpI = fopen(lc1Buf, "r");
	sprintf(lc1Buf, ".\\Test\\A%d.txt", siTNo);
	szpFpA = fopen(lc1Buf, "r");
	siRes = 0;
#else
	szpFpI = stdin;
#endif

	// 実行メイン
	llRet = fMain();

	// 出力
	sprintf(lc1Buf, "%lld\n", llRet);
	fOut(lc1Buf);

	// 残データ有無
#ifdef D_TEST
	lc1Buf[0] = '\0';
	fgets(lc1Buf, sizeof(lc1Buf), szpFpA);
	if (strcmp(lc1Buf, "")) {
		siRes = -1;
	}
#endif

	// テストファイルクローズ
#ifdef D_TEST
	fclose(szpFpI);
	fclose(szpFpA);
#endif

	// テスト結果
#ifdef D_TEST
	if (siRes == 0) {
		printf("OK %d\n", siTNo);
	}
	else {
		printf("NG %d\n", siTNo);
	}
#endif

	return 0;
}

// プログラム開始
int
main()
{

#ifdef D_TEST
	int i;
	for (i = D_TEST_SNO; i <= D_TEST_ENO; i++) {
		siTNo = i;
		fOne();
	}
#else
	fOne();
#endif

	return 0;
}