可爱STL,嘟嘟嘟!
xiaozhengguoaaa · · 题解
可以当作一个简单的队列题来做。
也有可能我太蒟蒻了,一眼想到了队列。
题目还是很好理解的。
主要思路
-
读入两个字符串,并用队列存储。
-
遍历整个
D 字符串,如果S 字符串为空,则跳出循环,如果两个字符串的第一个字符不同,则删除D 字符串的第一个元素,如果两个字符串的第一个字符相同,则删除 -
将答案加
1 。 -
如果
S 字符队列不是空的,则输出-1 ,否则输出答案。
AC code
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define fst ios::sync_with_stdio ( false ) ; cin.tie (0) ; cout.tie (0) ;
using namespace std ;
const int Mod = 998244353 ;
const int N = 5e6 + 7 ;
const int M = 1e5 ;
string o , p ;
queue<char > s , s1 ;
signed main ()
{
fst ;
cin >> o >> p ;
for (int i = 0 ; i < o.size() ; i ++)
{
s.push(o[i]) ;
}
for (int i = 0 ; i < p.size() ; i ++)
{
s1.push(p[i]) ;
}
int ans = 0 ;
while (!s1.empty())
{
if (s.empty())
{
break ;
}
if (s.front() != s1.front())
{
s1.pop() ;
}
else
{
s.pop() ;
s1.pop() ;
}
ans ++ ;
}
if (!s.empty())
{
ans = -1 ;
}
cout << ans ;
return 0 ;
}