跳到正文

题解

P9940

xu222ux约 1 分钟
原文
文章信息与版本

文章信息

发布
2023年12月17日
收录
2026年5月18日
更新
2026年11月11日
篇幅
约 1 分钟
题目
P9940

版本

  1. v1当前

P9940

贪心思路

看看听到字母的顺序,如果第 ii 个字母的出现位置比第 i−1i-1 个字母出现位置靠前,那么一定又多唱了一首歌。

code

C++
#include<bits/stdc++.h>
using namespace std;
string s,t;
int tmp[30];
int ans=1,q;
int main(){
	cin>>t>>s;
	for(int i=0;i<26;i++)tmp[t[i]-'a']=i;
	for(int i=0;i<s.length();i++){
		if(i==0)continue;
		if(tmp[s[i]-'a']<=tmp[s[i-1]-'a'])ans++;//康康是否多唱了一首歌
	}
	cout<<ans;
	return 0;
}
原文
存档评论2 条抓取于