CodeForces - 508B-Anton and currency you all know

本文介绍了一种算法,用于解决寻找从当前奇数汇率通过交换两个不同数字变为最大偶数汇率的问题。通过分析输入的汇率数字串,算法首先尝试找到小于末位奇数的第一个偶数进行交换,若不存在,则选择最后一个偶数进行交换。若汇率中无偶数,则输出-1。此题还强调了strlen函数的时间复杂度为O(n),避免将其放在循环中以防止超时。

Berland, 2016. The exchange rate of currency you all know against the burle has increased so much that to simplify the calculations, its fractional part was neglected and the exchange rate is now assumed to be an integer.

Reliable sources have informed the financier Anton of some information about the exchange rate of currency you all know against the burle for tomorrow. Now Anton knows that tomorrow the exchange rate will be an even number, which can be obtained from the present rate by swapping exactly two distinct digits in it. Of all the possible values that meet these conditions, the exchange rate for tomorrow will be the maximum possible. It is guaranteed that today the exchange rate is an oddpositive integer n. Help Anton to determine the exchange rate of currency you all know for tomorrow!

Input

The first line contains an odd positive integer n — the exchange rate of currency you all know for today. The length of number n's representation is within range from 2 to 105, inclusive. The representation of n doesn't contain any leading zeroes.

Output

If the information about tomorrow's exchange rate is inconsistent, that is, there is no integer that meets the condition, print  - 1.

Otherwise, print the exchange rate of currency you all know against the burle for tomorrow. This should be the maximum possible number of those that are even and that are obtained from today's exchange rate by swapping exactly two digits. Exchange rate representation should not contain leading zeroes.

Examples

Input

527

Output

572

Input

4573

Output

3574

Input

1357997531

Output

-1

题解:找小于末尾奇数的第一个偶数,如果没有,选择最后一个偶数,没偶数则输出-1

通过这道题,我明白了strlen的时间复杂度为O(n),如果放循环内,相当于时间复杂度为O(len*n)就容易超时,所以要提前接出放外面

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>

using namespace std;


int main() {

	char s[100005];
	scanf("%s",s);
	//sort(s,s+strlen(s));
	int k=-1;
	int flag=0;
	int minn;
	for(int t=0; t<strlen(s); t++) {
		if(s[t]-'0'<s[strlen(s)-1]-'0'&&(s[t]-'0')%2==0) {
			k=t;
			//	flag=1;
			break;
		}
	}
	//cout<<k<<endl;
	for(int t=0; t<strlen(s); t++) {
		if((s[t]-'0')%2==0) {
			minn=t;
			flag=1;
		}
	}
	//cout<<minn<<endl;
	if(flag==0) {
		cout<<"-1"<<endl;
		return 0;
	} else {
		for(int t=0; t<strlen(s); t++) {
			if(t==k) {
				swap(s[k],s[strlen(s)-1]);
				break;
			}
			if(k==-1) {
				swap(s[minn],s[strlen(s)-1]);
				break;

			}

		}
		//printf("%s",s);//也可通过 
		int len=strlen(s);
		for(int t=0; t<len; t++) {
			printf("%c",s[t]);
		}
	}

	return 0;
}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

black-hole6

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

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

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

打赏作者

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

抵扣说明:

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

余额充值