CF265A题解

题目:

传送门

思路:

模拟,用变量 \(t\) 表示松鼠当前所在的位置,循环遍历第二个字符串 \(b\),若 \(a_t = b_i\),则 \(t+1\),直到字符串 \(a\)\(b\) 被遍历完为止。注意: 如果字符串从 \(0\) 开始输入,则答案需 \(+1\),因为题目中松鼠是从第一块石头开始跳的。

代码:

#include <bits/stdc++.h>
  using namespace std;
int n,m,t,i;
char c[55],d[55];
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin>>c+1>>d+1;//从 1 开始输入,输出时不用 +1。 
	n=strlen(c+1);
	m=strlen(d+1);
	t=1;
	for (i=1;i<=m;i++)//循环遍历。 
	{
		if (c[t]==d[i]) t++;
		if (t==n+1) break;//如果字符串 a 遍历到底就退出循环。 
	}
	cout<<t<<endl;
	return 0;
}

posted @ 2022-05-21 22:53  Jason142  阅读(13)  评论(0编辑  收藏  举报