跳到正文

题解

可爱STL,嘟嘟嘟!

xiaozhengguoaaa约 2 分钟
原文
文章信息与版本

文章信息

收录
2026年4月8日
更新
2026年4月8日
篇幅
约 2 分钟
题目
P15854

版本

  1. v1当前

可以当作一个简单的队列题来做。

也有可能我太蒟蒻了,一眼想到了队列。

题目还是很好理解的。

主要思路

  • 读入两个字符串,并用队列存储。

  • 遍历整个 DD 字符串,如果 SS 字符串为空,则跳出循环,如果两个字符串的第一个字符不同,则删除 DD 字符串的第一个元素,如果两个字符串的第一个字符相同,则删除 DD 的第一个元素和 SS 的第一个元素。

  • 将答案加 11 。

  • 如果 SS 字符队列不是空的,则输出 −1-1,否则输出答案。

AC code

C++
#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 ;
}
原文