题目链接
题意:我将获得长度为N的字符串S,请你用 “nya” 替换字符串S中的连续的na。
思路:
遍历字符串,每遍历一次,就判断一次,是否合法!
代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 1e3 + 10;
char str[N];
int main()
{
int n;
cin >> n;
scanf ("%s", (str+1));
//naan
for (int i=1; i <= n; i ++)
{
if (str[i] == 'n' && str[i+1] == 'a') //判断相邻的两个字符是否是 “na”
{
cout << "nya";
i ++;
}
else cout << str[i];
}
return 0;
}