Hash Code Hacker

Hash Code Hacker
Time Limit: 4000ms, Special Time Limit:10000ms, Memory Limit:262144KB
 
Problem 13958 : Special judge
Problem description
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.
Sample Input
4
Sample Output
edHs
mENAGeS
fEHs
edIT

题面如上:题意就是说根据这个式子计算哈希值 s [0]*31^(n -1) + s [1]*31^(n -2) + ... + s[n -1],现在给出一个k,然后输出哈希值相同的k个string.

题解:思想很优秀,看到题解里的思想,绝对是很优秀。主要是因为自己太菜。

把这个hash当做一个31进制的数,那么随意生成一个字符串, 在高位减一,在低位加上 31,hash结果是一样的 
所以生成一个长度为 1000的字符串,然后逐位进行这样的操作 
就正好能构造出 1000个hash值相等的字符串 
然后输出前 K个即可

代码:

#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <vector>
#include <queue>
#include <stack>
#include <iostream>
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
using namespace std;
#define ll long long
#pragma comment(linker,"/STACk:10240000,10240000")
const int maxn=1e3+10;
char str[maxn];
int  main()
{
    int k;
    scanf("%d",&k);
    for(int i=0;i<1000;i++) str[i]='X';
    puts(str);
    for(int i=0;i<k-1;i++)
    {
        str[i]-=1;str[i+1]+=31;
        puts(str);
        str[i]+=1,str[i+1]-=31;
    }
    return 0;
}


转自大神博客:http://blog.csdn.net/u012015746/article/details/52474714

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值