Lucky Transformation (4 7转换)规律题

Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.

Petya has a number consisting of n digits without leading zeroes. He represented it as an array of digits without leading zeroes. Let’s call it d. The numeration starts with 1, starting from the most significant digit. Petya wants to perform the following operation k times: find the minimum x (1 ≤ x < n) such that dx = 4 and dx + 1 = 7, if x is odd, then to assign dx = dx + 1 = 4, otherwise to assign dx = dx + 1 = 7. Note that if no x was found, then the operation counts as completed and the array doesn’t change at all.

You are given the initial number as an array of digits and the number k. Help Petya find the result of completing k operations.

Input
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 109) — the number of digits in the number and the number of completed operations. The second line contains n digits without spaces representing the array of digits d, starting with d1. It is guaranteed that the first digit of the number does not equal zero.

Output
In the single line print the result without spaces — the number after the k operations are fulfilled.

Examples
Input
7 4
4727447
Output
4427477
Input
4 2
4478
Output
4478
Note
In the first sample the number changes in the following sequence: 4727447 → 4427447 → 4427477 → 4427447 → 4427477.

In the second sample: 4478 → 4778 → 4478.
哇,这次学长不知道怎么了,和4 7赶上了。这个题目就是在一个字符串里找寻47,找到之后,如果4的位置是奇数,就将47改为44,否则就将47改为77。在后一种情况下,需要注意,这是这个题里最大的坑,如果没有改之前是447,那么就将陷入一个循环,2333。这个题目是改完之后,从字符串头开始遍历。若是447,之后便是477,447,477,447,如此循环下去。这样我们就没有必要再循环了,直接找寻k的奇偶性,在那个位置直接改掉就可以了。如果k是偶数447中间的4 的位置就改为4,否则改为7。这样就ok了,不会超时,92ms就ac了。
代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;

const int maxx=1e5+10;
char a[maxx];
int n,k;

int main()
{
	while(cin>>n>>k)
	{
		getchar(); 
		gets(a);
		for(int i=0;i<n&&k;i++)
		{
			if(a[i]=='4'&&a[i+1]=='7')
			{
				if((i+1)&1) a[i+1]='4';
				else if(!((i+1)&1))
				{
					if(a[i-1]=='4')
					{
						if(k&1) a[i]='7';
						else a[i]='4';
						break;
					}
					else a[i]='7';
				}
				--k;
			}
		}
		puts(a);
	}
	return 0;
}

努力加油a啊,(o)/~。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值