Sdutoj Crack mathmen (analog, hash table, fast power)

Source: Internet
Author: User

Topic Connection: Portal

This is the problem of our provincial training yesterday, I can give a hole. But fortunately did not give the white hole, learned some things. The most touching thing is this.

for (int i = 0; i < strlen (str); i++)   //Danger, do not imitate

If the array is larger, then the write will be timed out directly. I have been looking for a long time not to find, finally seniors told me to write him this way

int len = strlen (str); for (int i = 0; i < len; i++)


Why is it? The reason is that if the array is large, it is necessary to calculate the length of STR of strlen (str), which can cause a timeout. But if you save it in an integer variable, it's good to just calculate the length of the Str once. This will not time out. Feel oneself in an instant to rise posture, before possibly because the array is relatively small, did not notice this, this time the array is a bit big, I have suffered from the pain. Everyone remember Ah, or prepare for a night time to make mistakes.

The hash table does not sound very big on Ah, it took so long, now know that the thing called hash table. But the hash table is really a good thing, he can make your search time is O (1), why? Because he can find it once. Let me say a hash table, you look, you suddenly startled, how is this thing.

Hash table (text version):

in many data structures (linear tables, trees, etc.), the relative position of the records in the structure is random, and there is no deterministic relationship between the recorded keywords, so a series of comparisons with the keywords are required to find records in the structure. This type of lookup method is based on comparison, so the efficiency of its lookup depends on the number of comparisons. The ideal is, of course, without any comparison, to produce results at once. It is necessary to establish a definite correspondence between the storage location of the record and his keyword F, which corresponds to a unique storage location in each of the keywords and structures. So as long as the lookup is based on the corresponding relationship F, we can find the value of K-like F (k). If there is a record in the structure where the keyword is equal to K, it must be in the storage location of f (k), so we can find the record at one time. We call this a hash function. We often use the table method in the use of the combination, so the hash table was born.

hash Table (expression version): f (key1) = Key2 (This expression is not fixed, I would like to say that the focus is to establish a mapping relationship, whether your expression is a function, or two times, three times is OK, this depends on the need to change)

———————————————————— Split Line ————————————————

Nonsense a lot, we now look at the problem. The topic you want to do is to restore a bunch of ciphertext. For the subject, because there is an n-th problem so we have to use the fast power. Do not know the fast power of the Please: here

This problem we can deal with this, the simulation of his encryption method will be, he used the characters are encrypted, all of them to save, and then we based on the cipher one by one comparison, we can use a hash table, to improve efficiency. Of course not to use this problem seems to be able to, but in time that is .... So let's use a hash table, so it's quicker and easier. In the end, we'll finish the problem by dealing with the details of the problem. For the case of no solution, consider that the first is to delete the multiple solutions and then delete the characters that are no longer used. This problem is a great success.

The code is as follows:

#include <stdio.h> #include <stdlib.h> #include <string.h> #define MOD 997#define MAXN 1000000 +    100struct n{int x; char c;} LIST_CHAR[62]; Use the struct to store the characters, their corresponding encrypted text, into the struct array char STR[MAXN],OUTSTR[MAXN/3]; STR is the input text, OUTSTR is the output text int asc[maxn/3],hash[997],flog;    ASC saves the encrypted text for each letter, the hash is the encrypted ciphertext and its array subscript hash table int Mod_pow (int x, int n) {//fast power int res = 1;        while (n > 0) {if (N & 1) res = res * x% MOD;        x = x * x% MOD;    n >>= 1; } return res;    int CMP (const void* A, const void* b) {struct n a = (struct n) A;    struct n = b = (struct n) b; if (a->x = = b->x) flog = 1; If in multiple solutions, this flog = 1 return a->x-b->x;}    int main () {int T, n, Len;    int I, J;        Save the character in the struct for (i = 0; i < i++) {if (i <) list_char[i].c = ' 0 ' + i;        else if (i < list_char[i].c) = ' A ' + i-10;    else list_char[i].c = ' a ' + i-36;    } scanf ("%d", &t); while (T--&AMP;&AMP;SCANF ("%d", &n)) {flog = 0;        Initialized to 0 memset (hash,0,sizeof (Hash)); for (i = 0; i < i++) list_char[i].x = Mod_pow ((int) list_char[i].c,n);        Converting characters into ciphertext into a structure qsort (list_char,62,sizeof (list_char[0]), CMP);        for (i = 0;!flog && i < i++)//If not in multiple solutions, this establishes hash table hash[list_char[i].x] = i + 1;        GetChar ();        scanf ("%s", str);        len = strlen (str); for (i = 0, j = 0; J < len/3 && i + 2 < Len; I+=3, J + +) Asc[j] = (str[i]-' 0 ') *100 + (str[i+1]        -' 0 ') *10 + str[i+2]-' 0 '; for (i = 0, j = 0; i < LEN/3; i++) {if (Hash[asc[i]]) outstr[j++] = List_char[hash[asc[i]]-            1].C;   else flog = 1;        Incorrect redaction} if (flog) printf ("No solution\n");            else{for (i = 0; i < J; i++) printf ("%c", Outstr[i]);        printf ("\ n"); }} return 0;}
(If there is an error, please correct it, reproduced in the source)



Sdutoj Crack mathmen (analog, hash table, fast power)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.