标签:main hash 内存 超过 词典 txt const sig bre
[expelliarmus] the disarming charm [rictusempra] send a jet of silver light to hit the enemy [tarantallegra] control the movement of one‘s legs [serpensortia] shoot a snake out of the end of one‘s wand [lumos] light the wand [obliviate] the memory charm [expecto patronum] send a Patronus to the dementors [accio] the summoning charm @END@ 4 [lumos] the summoning charm [arha] take me to the skySample Output
light the wand accio what? what?
题意 : 给你一个咒语对应着一个功能,要求有 q 此询问,对于每次询问给出相应的咒语后告诉你相应的功能
思路分析:这题用 map 可能会超内存,因此我们这里用 hash 去做,对于 hash 后的值相同的情况下我们去连一条链,然后匹配的时候去在这条链上去匹配即可。
代码示例:
using namespace std; #define ll unsigned long long const ll maxn = 1e6+5; char s[200]; struct node { char que[25]; char ans[85]; int next; }a[maxn], b[maxn]; ll p = 100007; char que_[25], ans_[85]; int ha[maxn], hb[maxn]; ll cura=0, curb=0; ll gethash(char *str){ ll res = 0; for(ll i = 0; *(str+i); i++){ res = res*p+(str[i]-‘a‘); } return res%p; } void insert(){ ll hash_a = gethash(que_); strcpy(a[cura].que, que_); strcpy(a[cura].ans, ans_); a[cura].next = ha[hash_a]; ha[hash_a] = cura; cura++; ll hash_b = gethash(ans_); strcpy(b[curb].que, que_); strcpy(b[curb].ans, ans_); b[curb].next = hb[hash_b]; hb[hash_b] = curb; curb++; //printf("++++ %llu %llu \n", hash_a, hash_b); } bool searcha(char *str){ ll num = gethash(str); int x= ha[num]; //printf("1111111111 %llu %d\n", num, x); while(x != -1){ //printf("_______ %s \n", a[x].que); if (strcmp(a[x].que, str) == 0){ printf("%s\n", a[x].ans); return true; } x = a[x].next; } return false; } bool searchb(char *str){ ll num = gethash(str); int x= hb[num]; //printf("2222222222 %llu %llu\n", num, x); while(x != -1){ if (strcmp(b[x].ans, str) == 0){ printf("%s\n", b[x].que); return true; } x = b[x].next; } return false; } int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); memset(ha, -1, sizeof(ha)); memset(hb, -1, sizeof(hb)); while(1){ gets(s+1); ll len = strlen(s+1); ll pos = 1; if (s[1] == ‘@‘) break; ll k = 0; for(ll i = 2; i <= len; i++){ if (s[i] == ‘]‘) {pos = i; break;} que_[k++] = s[i]; } que_[k] = ‘\0‘; k = 0; for(ll i = pos+2; i <= len; i++){ ans_[k++] = s[i]; } ans_[k] = ‘\0‘; insert(); } ll q; cin >>q; getchar(); while(q--){ gets(s); ll len = strlen(s); if (s[0] == ‘[‘){ s[len-1] = ‘\0‘; s[0] = ‘\0‘; if (!searcha(s+1)) printf("what?\n"); } else { if (!searchb(s)) printf("what?\n"); } } return 0; }
标签:main hash 内存 超过 词典 txt const sig bre
原文地址:https://www.cnblogs.com/ccut-ry/p/9380592.html