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;
}