【substr】#76 A. Restoring Password

本文介绍了一个密码恢复的问题,黑客通过病毒更改了ISQ用户的密码,并留下了一串80字符的二进制代码。文章提供了恢复密码的方法,即通过匹配10位二进制代码组与数字0到9的对应关系来还原原始的8位数字密码。

A. Restoring Password
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Igor K. always used to trust his favorite Kashpirovsky Antivirus. That is why he didn't hesitate to download the link one of his groupmates sent him via QIP Infinium. The link was said to contain "some real funny stuff about swine influenza". The antivirus had no objections and Igor K. run the flash application he had downloaded. Immediately his QIP Infinium said: "invalid login/password".

Igor K. entered the ISQ from his additional account and looked at the info of his main one. His name and surname changed to "H1N1" and "Infected" correspondingly, and the "Additional Information" field contained a strange-looking binary code 80 characters in length, consisting of zeroes and ones. "I've been hacked" — thought Igor K. and run the Internet Exploiter browser to quickly type his favourite search engine's address.

Soon he learned that it really was a virus that changed ISQ users' passwords. Fortunately, he soon found out that the binary code was actually the encrypted password where each group of 10 characters stood for one decimal digit. Accordingly, the original password consisted of 8 decimal digits.

Help Igor K. restore his ISQ account by the encrypted password and encryption specification.

Input

The input data contains 11 lines. The first line represents the binary code 80 characters in length. That is the code written in Igor K.'s ISQ account's info. Next 10 lines contain pairwise distinct binary codes 10 characters in length, corresponding to numbers 0, 1, ..., 9.

Output

Print one line containing 8 characters — The password to Igor K.'s ISQ account. It is guaranteed that the solution exists.

Sample test(s)
input
01001100100101100000010110001001011001000101100110010110100001011010100101101100
0100110000
0100110010
0101100000
0101100010
0101100100
0101100110
0101101000
0101101010
0101101100
0101101110
output
12345678
input
10101101111001000010100100011010101101110010110111011000100011011110010110001000
1001000010
1101111001
1001000110
1010110111
0010110111
1101001101
1011000001
1110010101
1011011000
0110001000
output
30234919


题目名就告诉你们这个题目叫做【存密码】

给一个长字符串,然后给你0-9的表示方法,问刚刚那个字符串是怎样一个8位数

我们就用str[i] i=0~9 来存储每个数字代表的串咯~

然后怎么匹配呢,我们可以用substr这个函数 str.substr(pos,len) 是代表从str字符串的pos开始,获得一个长度为len的字符串,这是一个字符串问题常用的很方便快捷的函数。

那么来看看源码?

#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring> 
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
// http://codeforces.com/contest/94
// Restoring Password
int main()
{
	string acinfo;	cin>>acinfo;
	string s[10];
	for(int i=0;i<10;i++)	cin>>s[i];
	for(int i=0;i<8;i++)
	{
		string tmp=acinfo.substr(i*10,10);
		//cout<<tmp<<endl;
		for(int i=0;i<10;i++)
		{
			if(tmp==s[i])
			{
				cout<<i;
				break;
			}
		} 
	}
	
	return 0;
} 




`TO_NUMBER(SUBSTR(a.description, LENGTH(a.description) - 5, 6))` 是一个SQL函数组合,用于从字符串 `a.description` 中提取最后6位字符,并将其转换为数字。以下是逐步解析: --- ### 1. **`SUBSTR(a.description, LENGTH(a.description) - 5, 6)`** - **作用**:从字符串 `a.description` 中截取子串。 - **参数**: - **`a.description`**:源字符串(例如 `F42000001`)。 - **`LENGTH(a.description) - 5`**:起始位置。 - `LENGTH(a.description)` 计算字符串的总长度(如 `F42000001` 的长度是9)。 - `-5` 表示从倒数第6个字符开始(因为 `9 - 5 = 4`,即从第4个字符开始,但SQL中字符串位置从1开始)。 - **`6`**:截取长度(即提取6个字符)。 - **示例**: - 若 `a.description = &#39;F42000001&#39;`(长度9): - 起始位置:`9 - 5 = 4`(从第4个字符 `&#39;0&#39;` 开始)。 - 截取结果:`&#39;000001&#39;`(第4到第9位)。 - 若 `a.description = &#39;F42123456&#39;`(长度9): - 截取结果:`&#39;123456&#39;`。 --- ### 2. **`TO_NUMBER(...)`** - **作用**:将截取的字符串转换为数字类型。 - **示例**: - `TO_NUMBER(&#39;000001&#39;)` → 数字 `1`。 - `TO_NUMBER(&#39;123456&#39;)` → 数字 `123456`。 - **注意**: - 如果截取的字符串包含非数字字符(如 `&#39;00A001&#39;`),会抛出 **“无效的数字”** 错误。此时需先用 `REGEXP_REPLACE` 清理数据(见下文)。 --- ### 3. **完整逻辑** 这段代码的目的是: **从 `description` 字段中提取最后6位数字,并找到所有记录中的最大值**。 - 假设 `description` 格式为 `F42` 开头 + 6位数字(如 `F42000001`),则直接截取后6位即可。 - 如果格式可能有变化(如包含分隔符 `F42-000001`),需先清理非数字字符。 --- ### 4. **潜在问题及修复** #### 问题1:字符串长度不足6位 - 如果 `description` 长度小于6(如 `F4212`),`SUBSTR` 会返回实际能截取的部分(如 `&#39;F4212&#39;`),但 `TO_NUMBER` 可能失败。 - **修复**:添加长度检查 `AND LENGTH(a.description) >= 6`。 #### 问题2:非数字字符 - 如果 `description` 是 `F42-00A001`,截取的 `&#39;0A001&#39;` 无法转换为数字。 - **修复**:用 `REGEXP_REPLACE` 清理非数字字符: ```sql TO_NUMBER(REGEXP_REPLACE(SUBSTR(a.description, LENGTH(a.description) - 5, 6), &#39;[^0-9]&#39;, &#39;&#39;)) ``` --- ### 5. **优化后的代码** ```sql SELECT MAX( TO_NUMBER(REGEXP_REPLACE( SUBSTR(a.description, GREATEST(LENGTH(a.description) - 5, 1), 6), &#39;[^0-9]&#39;, &#39;&#39; )) ) AS MaxLastSixDigits FROM ptBag a INNER JOIN a_packingtype b ON a.packingtypeid = b.packingtypeid WHERE a.description LIKE &#39;F42%&#39; AND b.packingtypename != &#39;OuterBox&#39; AND LENGTH(a.description) >= 1; ``` - **`GREATEST(..., 1)`**:确保起始位置不小于1(避免负数位置)。 - **`REGEXP_REPLACE`**:删除所有非数字字符。 --- ### 6. **调试建议** 运行以下查询检查中间结果: ```sql SELECT a.description, SUBSTR(a.description, GREATEST(LENGTH(a.description) - 5, 1), 6) AS raw_substr, REGEXP_REPLACE(SUBSTR(a.description, GREATEST(LENGTH(a.description) - 5, 1), 6), &#39;[^0-9]&#39;, &#39;&#39;) AS cleaned_substr FROM ptBag a INNER JOIN a_packingtype b ON a.packingtypeid = b.packingtypeid WHERE a.description LIKE &#39;F42%&#39; AND b.packingtypename != &#39;OuterBox&#39;; ``` --- ### 总结 - **原始代码**:假设 `description` 的最后6位是纯数字,直接截取并转换。 - **健壮性改进**:处理长度不足、非数字字符等情况。 - **关键点**:`SUBSTR` 的位置计算和 `TO_NUMBER` 的数据清洗。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

糖果天王

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

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

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

打赏作者

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

抵扣说明:

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

余额充值