洛谷 P1603 斯诺登的密码

本文介绍了一种通过字符串转换为数值、排序及全排列来找出最小组合值的算法实现过程。该算法首先将输入的字符串根据预设的映射表转化为数值,并计算其平方后取模,接着对转化后的数值进行排序,再通过全排列找出所有可能的组合,最后通过比较输出最小值。
摘要由CSDN通过智能技术生成



1.打表

2.搜索并转换

3.排序

4.全排列所有组合

5.比较出最小值

6.输出



#include <iostream>
#include <algorithm>
using namespace std;

int trans(string s)
{
	long long ret = 0;
	
	for(int i = s.length(), ps = 10; i >= 0; i--, ps *= 10)
	{
		ret += ( (s[i] - '0') * ps ) / 10;
	}
	ret *= ret;
	ret %= 100;
	return ret;
}

int main()
{
	string rstr[22] = {"","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty"};
	string ustr[4] ={"","a","both","another"};
	string tstr[4] = {"","first","second","third"};
	string txt;
	
	int narr[10] = {0};
	int flag = 0;
	int tf = 0;
	
	while(cin>>txt && txt != ".")
	{
		if(txt != "0")
		{
			for(int i = 0; i < 22; i++)
			{
				if(rstr[i] == txt)
				{
					narr[tf++] = (i * i) % 100;
					goto quit;
				}
			}
			for(int i = 0; i < 4; i++)
			{
				if(ustr[i] == txt)
				{
					narr[tf++] = (i * i) % 100;
					goto quit;
				}
			}
			for(int i = 0; i < 4; i++)
			{
				if(tstr[i] == txt)
				{
					narr[tf++] = (i * i) % 100;
					goto quit;
				}
			}
		}
		quit:;
	}
	
	sort(narr, narr + tf);
	
	
	/*for(int i = 0; i < tf; i++)
		cout<<narr[i]<<' ';*/
		
	long long ans = 9999999999999;
	
	do
	{
		long long tans = 0;
		long long ps = 10;
		for(int i = 0; i < tf; i++)
		{
			int tmp = narr[i];
			tans += ( (tmp % 10) * ps ) / 10;
			ps *= 10;
			tans += ( ( (tmp / 10) % 10) * ps ) / 10;
			ps *= 10;
			//cout<<tans<<endl;
		}
		ans = min(ans, tans);
	}while(next_permutation(narr, narr + tf));
	
	cout<<ans<<endl;
	return 0;
} 





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值