其他分享
首页 > 其他分享> > P1321 单词覆盖还原

P1321 单词覆盖还原

作者:互联网

在这里插入图片描述

代码

就是按照优先级(girl>gir,irl>gi,ir,rl>a,i,r,l和boy>bo,oy>b,o,y)的顺序,注意查找之后i要相应的跳过这个串。

#include<bits/stdc++.h>

using namespace std;

int main(){
	string s;
	int i,cnt_b=0,cnt_g=0;
	cin>>s;
	for(i=0;i<s.length();i++){
		if(s[i]=='g'&&s[i+1]=='i'&&s[i+2]=='r'&&s[i+3]=='l'){
			cnt_g++;i+=3;
		}else if(s[i]=='b'&&s[i+1]=='o'&&s[i+2]=='y'){
			cnt_b++;i+=2;
		}else if((s[i]=='g'&&s[i+1]=='i'&&s[i+2]=='r')||(s[i]=='i'&&s[i+1]=='r'&&s[i+2]=='l')){
			cnt_g++;i+=2;
		}
		else if((s[i]=='b'&&s[i+1]=='o')||(s[i]=='o'&&s[i+1]=='y')){
			cnt_b++;i+=1;
		}else if((s[i]=='g'&&s[i+1]=='i')||(s[i]=='i'&&s[i+1]=='r')||(s[i]=='r'&&s[i+1]=='l')){
			cnt_g++;i+=1;
		}else if(s[i]=='b'||s[i]=='o'||s[i]=='y') cnt_b++;
		else if(s[i]=='g'||s[i]=='i'||s[i]=='r'||s[i]=='l') cnt_g++;
	}
	cout<<cnt_b<<endl<<cnt_g;
	return 0;
}

标签:cnt,cout,++,else,单词,int,P1321,还原,&&
来源: https://blog.csdn.net/Z_H86/article/details/117202071