第二次组队赛补题--Problem H. Hash Code Hacker

Input file: hash.in
Output file: hash.out
Time limit: 2 seconds
Memory limit: 256 megabytes
According to Java standard library documentation, the hash code of String is computed as
s [0]*31^( n -1) + s [1]*31^( n -2) + … + s [n -1]
Here s[i] is the i-th character of the string, n is the length of the string, and ^ indicates exponentiation.
Computation uses signed 32-bit integers in two’s complement form.
Heather is going to hack the servers of Not Entirely Evil Recording Company (NEERC). To perform
an attack she needs k distinct query strings that have equal hash codes. Unfortunately, NEERC servers
accept query string containing lower- and uppercase English letters only.
Heather hired you to write a program that generates such query strings for her.
Input
The single line of the input file contains integer k — the number of required query strings to generate
(2 ≤ k ≤ 1000).
Output
Output k lines. Each line should contain a single query string. Each query string should be non-empty
and its length should not exceed 1000 characters. Query string should contain only lower- and uppercase
English letters. All query strings should be distinct and should have equal hash codes.
Example
hash.in hash.out
4 edHs
mENAGeS
fEHs
edIT
题意:给定一个字符串,规定字符串的哈希码的计算公式为:s [0]*31^( n -1) + s [1]*31^( n -2) + … + s [n -1],给定一个整数k(2<=k<=1000)要求给出k个哈希码相同的字符串(长度不限)
解题思路:这个题目的解题思路是我的队友想出来的,我当时觉得非常棒,所以写个博客把这个思路记录下来;思路:
s [0]*31^( n -1) + s [1]*31^( n -2) + … =( s[0]+1-1)*31^( n -1) + s [1]*31^( n -2) + …=(s [0]+1)*31^( n -1)- 31^( n -1) + s [1]*31^( n -2) + …=(s [0]+1)*31^( n -1)+ (s [1]-31)*31^( n -2) + …
这样的话每次改变相邻的两个字母,前一个字母加一,后一个字母减31就得到一个新的和原字符串哈希码相等的字符串,由于k最大等于1000,字符串长度最长不超过1000,这样吧字符串的所有字母全设置为B或C。。。。。。长度为一千,加上本身正好可以最多形成1000个具有相同哈希码的不同字符串,是不是很棒的思路。
代码如下:
#include
#include
#include
#include
using namespace std;
char s[1001];
int book[10100];
int main()
{
freopen(“hash.in”,“r”,stdin);
freopen(“hash.out”,“w”,stdout);
int n;
cin>>n;
int i=-1;
while(n–)
{
for(int i=0; i<1000; i++)
{
s[i]=‘B’;
}
if(i==-1)
{
cout<<s<<endl;
i++;
}
else
{
for(int j=i; j<999; j++)
{
s[j]–;
s[j+1]+=31;
break;
}
cout<<s<<endl;
i++;

    }
}
return 0;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值