题解:P17232 [Algo Beat Contest 017 A] 串哈希
蒟蒻的第一篇题解!!!
题目传送门
这道题是一道灰常简单的模拟题
前置芝士
在 C++ 中,要判断一个子串是否在 string 中出现过,最标准的方法是使用 find() 成员函数。
所以,我们可以用 find()来找字符串 s1.find(s2) 表示在 s1 中寻找 s2 ,如果找到了就返回 s2 第一个字母在 s1 中的下标,如果没找到就返回 string::npos 。 下面是一个基本实现
#include<bits/stdc++.h>
using namespace std;
string s1,s2;
void check(){
if(s1.find(s2)!=string::npos){
cout<<1;
return ;
}else{
cout<<0;
return ;
}
}
int main(){
cin >> s1 >> s2;
check();
return 0;
}
正式开始
通读整道题,发现 规则 4 的优先级最高,其次是 规则 3 ,然后是 规则 2 ,最后是 规则 1 。那我们代码的判断环节就得反着写。还有注意 t 不等于 s ,所以我们还得在一开始把 s 记录一下。
AC代码
#include<bits/stdc++.h>
using namespace std;
const string a="shuki";// 不会真有人把这些字符串写了好几遍吧
const string b="daishuki";
const string c="kirai";
int n,s;
string str;
bool check1(string str)//记得写 return false( 别问我怎么知道的 )
{
if (str.find(a)==string::npos)
{
if (str.find(b)==string::npos)
{
if (str.find(c)==string::npos){
return true;
}
}
}
return false;
}
bool check2(string str)
{
if (str.find(c)!=string::npos)
{
return true;
}
return false;
}
bool check3(string str)
{
if (str.find(b)!=string::npos)
{
return true;
}
return false;
}
bool check4(string str)
{
if (str.find(a)!=string::npos)
{
return true;
}
return false;
}
int main()
{
ios_base::sync_with_stdio(0);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);// 简陋的快读 UwU
cin >> n;
cin >> s;
int s1=s;
while(n--)
{
cin >> str;
if (check1(str))// 倒着判断
{
s--;
continue;
}else if (check2(str))
{
if (s>=0)// 注意规则 3 还有第二个判断 OwO
{
s=0;
continue;
}else
{
continue;
}
}else if (check3(str))
{
s+=2;
continue;
}else if (check4(str))
{
s++;
continue;
}
}
if (s>0)//记得判断啊 QwQ
{
s=s-s1;
cout<<s<<endl;
}else
{
cout<<a<<endl;
}
return 0;// 咕咕嘎嘎
}
☆: .。. o(≧▽≦)o .。.:☆ 拜拜!!!