F.Promote Code---有技巧的模拟

博客围绕促销码加密恢复问题展开,limojin想通过恢复Wells发来的加密促销码低价买Switch。已知加密流程,Wells忘记部分加密算法。题目给出输入输出要求及示例,还提到这是有技巧的暴力模拟题,介绍了恢复促销码的思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Promote Code

Time Limit: 1 Sec Memory Limit: 128 Mb

题目链接http://acm.csu.edu.cn:20080/csuoj/problemset/problem?pid=2329

Description

一天,limojin在eBay上浏览着喜爱的商品,尽管如此,一个Switch对他来说还是太贵了。limojin了解到一种按照升序排列的数字串,称为促销码(promote code),可以使他以更低的价格购入一个Switch。这个时候,Wells神秘兮兮地发过来一串字符。(“NeNohuiroNNiNeteefrsixe”) Wells声称这串字母是他将一串促销码经过加密得到的。

现在已知加密的流程如下:

1.用数字的英文来代替数字(如:134699 -> onethreefoursixninenine)

2.用加密算法对上步得到的字符串进行处理。

不幸的是,健忘的Wells忘记了2中加密算法的具体步骤,只记得加密算法仅仅改变了字符串中字母的排列顺序和大小写。(如:onethreefoursixninenine ->NeNohuiroNNiNeteefrsixe)

对于Wells发来的字符串,你能帮帮limojin恢复促销码吗?limojin能否成功买到开关(switch)来修理家中电器取决于你的决定!

Input

多组数据,数据组数不超过100组,每组数据一行

每行表示一个Wells发送过来的经过加密的促销码

Output

每组数据输出一行表示答案

保证答案长度在1000位以内

Sample Input

onethreefoursixninenine
NeNohuiroNNiNeteefrsixe

Sample Output

134699
134699


emmm,这是一个有技巧的暴力模拟题。。。
我们先将所有字母转化为小写,然后我们从零开始扣:

while (nb['z'-'a'] && nb['e'-'a'] && nb['r'-'a'] && nb['o'-'a']) {
	a[0]++;
	nb['z'-'a']--;
	nb['e'-'a']--;
	nb['r'-'a']--;
	nb['o'-'a']--;
}

以此类推。。。然后我们发现答案是11369(好像是。。。)然后看看没有开SpeciallJudge。。。也就是说我们的想法有误,必须要将字母全部用上。。。那么我们只能找特殊的了,零独有‘z’,2独有‘w’,4独有‘u’,6独有‘x’:

while (nb['z'-'a'] && nb['e'-'a'] && nb['r'-'a'] && nb['o'-'a']) {
	int p=nb['z'-'a'];
	a[0]+=p;
	nb['z'-'a']-=p;
	nb['e'-'a']-=p;
	nb['r'-'a']-=p;
	nb['o'-'a']-=p;
}

删去这些之后又有一些字母是剩下每个数独有的然后就这样一直下去就好了。。。

以下是AC代码:

#include <bits/stdc++.h>
using namespace std;
char s[10005];
int nb[60],a[1005];
int main()
{
	while (~scanf ("%s",s)){
		int len=strlen(s);
		memset(nb,0,sizeof(nb));
		for (int i=0; i<len; i++) if (s[i]>='A' && s[i]<='Z') s[i]+=32;
		for (int i=0; i<len; i++)
		  nb[s[i]-'a']++;
		while (nb['z'-'a'] && nb['e'-'a'] && nb['r'-'a'] && nb['o'-'a']) {
			int p=nb['z'-'a'];
			a[0]+=p;nb['z'-'a']-=p;nb['e'-'a']-=p;
			nb['r'-'a']-=p;nb['o'-'a']-=p;
		}
		while (nb['t'-'a'] && nb['w'-'a'] && nb['o'-'a']) {
			int p=nb['w'-'a'];
			a[2]+=p;nb['t'-'a']-=p;
			nb['w'-'a']-=p;nb['o'-'a']-=p;
		}
		while (nb['f'-'a'] && nb['u'-'a'] && nb['r'-'a'] && nb['o'-'a']) {
			int p=nb['u'-'a'];
			a[4]+=p;nb['f'-'a']-=p;nb['u'-'a']-=p;
			nb['r'-'a']-=p;nb['o'-'a']-=p;
		}
		while (nb['s'-'a'] && nb['i'-'a'] && nb['x'-'a']) {
			int p=nb['x'-'a'];
			a[6]+=p;nb['s'-'a']-=p;
			nb['i'-'a']-=p;nb['x'-'a']-=p;
		}
		while (nb['f'-'a'] && nb['i'-'a'] && nb['v'-'a'] && nb['e'-'a']) {
			int p=nb['f'-'a'];
			a[5]+=p;nb['f'-'a']-=p;nb['i'-'a']-=p;
			nb['v'-'a']-=p;nb['e'-'a']-=p;
		}
		while (nb['s'-'a'] && nb['e'-'a']>=2 && nb['v'-'a'] && nb['n'-'a']) {
			int p=nb['v'-'a'];
			a[7]+=p;nb['s'-'a']-=p;nb['n'-'a']-=p;
			nb['e'-'a']-=2*p;nb['v'-'a']-=p;
		}
		while (nb['i'-'a'] && nb['e'-'a'] && nb['g'-'a'] && nb['h'-'a'] && nb['t'-'a']) {
			int p=nb['g'-'a'];
			a[8]+=p;nb['i'-'a']-=p;nb['g'-'a']-=p;
			nb['e'-'a']-=p;nb['h'-'a']-=p;nb['t'-'a']-=p;
		}
		while (nb['t'-'a'] && nb['h'-'a'] && nb['r'-'a'] && nb['e'-'a']>=2) {
			int p=nb['h'-'a'];
			a[3]+=p;nb['t'-'a']-=p;nb['e'-'a']-=2*p;
			nb['r'-'a']-=p;nb['h'-'a']-=p;
		}	
		while (nb['e'-'a'] && nb['n'-'a'] && nb['o'-'a']) {
			int p=nb['o'-'a'];
			a[1]+=p;nb['e'-'a']-=p;
			nb['n'-'a']-=p;nb['o'-'a']-=p;
		}		
		while (nb['i'-'a'] && nb['e'-'a'] && nb['n'-'a']>=2) {
			int p=nb['i'-'a'];
			a[9]+=p;nb['i'-'a']-=p;nb['n'-'a']-=2*p;
			nb['e'-'a']-=p;
		}
		for (int i=0; i<=9; i++){
			while (a[i]){
				printf ("%d",i);a[i]--;
			}
		}
		printf ("\n");
	}
	return 0;
} 
/** * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the * following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following * disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the * following disclaimer in the documentation and/or other materials provided with the distribution. * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote * products derived from this software without specific prior written permission. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * @file mcs_pid_ctrl.c * @author MCU Algorithm Team * @brief This file provides functions of general PID controller */ #include "mcs_pid_ctrl.h" #include "mcs_math.h" #include "mcs_assert.h" /** * @brief Reset all member variables of PID controller to zero. * @param pidHandle PID controller struct handle. * @retval None. */ void PID_Reset(PidHandle *pidHandle) { MCS_ASSERT_PARAM(pidHandle != NULL); /* Reset the PID parameter. */ pidHandle->error = 0.0f; pidHandle->errorLast = 0.0f; pidHandle->feedforward = 0.0f; pidHandle->integral = 0.0f; pidHandle->saturation = 0.0f; pidHandle->differ = 0.0f; pidHandle->kp = 0.0f; pidHandle->ki = 0.0f; pidHandle->kd = 0.0f; pidHandle->ns = 0.0f; pidHandle->ka = 0.0f; /* Reset the Limiting Value. */ pidHandle->upperLimit = 0.0f; pidHandle->lowerLimit = 0.0f; } /** * @brief Clear historical values of PID controller. * @param pidHandle PID controller struct handle. * @retval None. */ void PID_Clear(PidHandle *pidHandle) { MCS_ASSERT_PARAM(pidHandle != NULL); /* Clear historical values of PID controller. */ pidHandle->differ = 0.0f; pidHandle->integral = 0.0f; pidHandle->saturation = 0.0f; pidHandle->feedforward = 0.0f; pidHandle->error = 0.0f; pidHandle->errorLast = 0.0f; } /** * @brief Execute simplified PI controller calculation, static clamping, no feedfoward. * @param pidHandle PI controller struct handle. * @retval PI control output. */ float PI_Exec(PidHandle *pidHandle) { MCS_ASSERT_PARAM(pidHandle != NULL); /* Proportional Item */ float p = pidHandle->kp * pidHandle->error; /* Integral Item */ float i = pidHandle->ki * pidHandle->error + pidHandle->integral; i = Clamp(i, pidHandle->upperLimit, pidHandle->lowerLimit); pidHandle->integral = i; /* static clamping and output calculaiton */ float val = p + i; float out = Clamp(val, pidHandle->upperLimit, pidHandle->lowerLimit); return out; } /** * @brief Execute PID controller calculation. dynamic clamping, feedfoward compenstaion * @param pidHandle PID controller struct handle. * @retval PID control output. */ float PID_Exec(PidHandle *pidHandle) { MCS_ASSERT_PARAM(pidHandle != NULL); /* Proportional Item */ float p = pidHandle->kp * pidHandle->error; /* Integral Item */ float i = pidHandle->ki * (pidHandle->error - pidHandle->ka * pidHandle->saturation) + pidHandle->integral; i = Clamp(i, Max(0.0f, pidHandle->upperLimit), Min(0.0f, pidHandle->lowerLimit)); pidHandle->integral = i; /* Differential Item */ float d = pidHandle->kd * pidHandle->ns * (pidHandle->error - pidHandle->errorLast) - \ pidHandle->differ * (pidHandle->ns - 1.0f); pidHandle->errorLast = pidHandle->error; pidHandle->differ = d; /* Output value update and saturation value calculation */ float val = p + i + d + pidHandle->feedforward; float out = Clamp(val, pidHandle->upperLimit, pidHandle->lowerLimit); pidHandle->saturation = val - out; return out; } /** * @brief Set the proportional parameter kp of PID controller. * @param pidHandle PID controller struct handle. * @param kp The proportional parameter. * @retval None. */ void PID_SetKp(PidHandle *pidHandle, float kp) { MCS_ASSERT_PARAM(pidHandle != NULL); MCS_ASSERT_PARAM(kp >= 0.0f); pidHandle->kp = kp; } /** * @brief Set the integral parameter ki of PID controller. * @param pidHandle PID controller struct handle. * @param ki The integral parameter. * @retval None. */ void PID_SetKi(PidHandle *pidHandle, float ki) { MCS_ASSERT_PARAM(pidHandle != NULL); MCS_ASSERT_PARAM(ki >= 0.0f); pidHandle->ki = ki; } /** * @brief Set the derivative parameter kd of PID controller. * @param pidHandle PID controller struct handle. * @param kd The derivative parameter. * @retval None. */ void PID_SetKd(PidHandle *pidHandle, float kd) { MCS_ASSERT_PARAM(pidHandle != NULL); MCS_ASSERT_PARAM(kd >= 0.0f); pidHandle->kd = kd; }
06-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值