AtCoder Beginner Contest 286——B - Cat

AtCoder Beginner Contest 286 题目讲解

A题 B题 C题 D题 E题


蒟蒻来讲题,还望大家喜。若哪有问题,大家尽可提!

Hello, 大家好哇!本初中生蒟蒻今天讲解一下AtCoder Beginner Contest 286的B题——Cat

===========================================================================================

原题

Problem Statement

You are given a string S S S of length N N N.

Find the string obtained by replacing all contiguous occurrences of n a na na in S with n y a nya nya.

Constraints

  • N N N is an integer between 1 1 1 and 1000 1000 1000, inclusive.
  • S S S is a string of length N N N consisting of lowercase English letters.

Input

The input is given from Standard Input in the following format:

N
S

Output

Print the answer.

Sample Input 1

4
naan

Sample Output 1

nyaan

Replacing all contiguous occurrences of n a na na in naan with n y a nya nya results in the string n y a a n nyaan nyaan.

Sample Input 2

4
near

Sample Output 2

near

S may not contain a contiguous na.

Sample Input 3

8
national

Sample Output 3

nyationyal

思路

循环并判断是否是 n a na na,若是输出 n y a nya nya,否则输出 s i s_{i} si


代码

#include 

using namespace std;

int n;
string s;

int main()
{
	cin >> n >> s;
	
	s = ' ' + s;
	
	for (int i = 1; i <= n; i ++)	
		if (s[i] == 'n' && s[i + 1] == 'a') //如果是
			cout << "nya", i ++; //因为下一个a不用判断了,直接i++
		else//不是
			cout << s[i];

	return 0;
}

今天就到这里了!

大家有什么问题尽管提,我都会尽力回答的!最后,除夕夜祝大家新年快乐!
在这里插入图片描述

吾欲您伸手,点的小赞赞。吾欲您喜欢,点得小关注!

你可能感兴趣的:(算法-暴力,算法,c++,数据结构)