#include<stdio.h> #include<iostream> #include<fstream> using namespace std; const int hash_size = 199; unsigned int ELFHash( char * str) { unsigned int hash = 0 ; unsigned int x = 0 ; while ( * str) { hash = (hash << 4 ) + ( * str ++ ); //hash值左移4位加上一个字符 if ((x = hash & 0xF0000000L ) != 0 )//判断hash值的高4位是否不为0,因为不为0时需要下面特殊处理,否则上面一步的左移4位会把这高四位给移走,造成信息丢失 { hash ^= (x >> 24 ); //把刚才的高4位跟hash的低5-8位异或 hash &= ~ x; //把高4位清0 } } return (hash & 0x7FFFFFFF ); //希望hash值是一个非负数 } struct Node{ unsigned val; Node * next; }; void SaveUniv() { int N; char ch; ofstream out_f; out_f.open(".//acm//A_large.out"); ifstream in_f; in_f.open(".//acm//A-large-practice.in");// test.txt");// if(!in_f){ exit(1); } //scanf("%d", &N); //scanf("%c", &ch); in_f>>N; int i, k, S, Q; for(i = 0; i < N; i++){ //scanf("%d", &S); //scanf("%c", &ch); in_f>>S; in_f.get(ch); char ** pEng = new char*[S]; for(k = 0; k < S; k++){ pEng[k] = new char[102]; } int len = 100, s_len; for(k = 0; k < S; k++){ // scanf("%c", &ch); in_f.get(ch); s_len = 0; while(ch != 10){ pEng[k][s_len++] = ch; // scanf("%c", &ch); in_f.get(ch); } //s_len = getline(&pEng[k], &len, stdin); // contains newline pEng[k][s_len] = 0; } //scanf("%d", &Q); //scanf("%c", &ch); in_f>>Q; in_f.get(ch); char ** pQry = new char*[Q]; for(k = 0; k < Q; k++){ pQry[k] = new char[102]; } for(k = 0; k < Q; k++){ //scanf("%c", &ch); in_f.get(ch); s_len = 0; while(ch != 10){ pQry[k][s_len++] = ch; //scanf("%c", &ch); in_f.get(ch); } //s_len = getline(&pEng[k], &len, stdin); // contains newline pQry[k][s_len] = 0; } // hash unsigned val, h_val; Node ** pHash = new Node*[hash_size]; for( k = 0; k < hash_size; k++) pHash[k] = NULL; int cnt = 0, switch_cnt = 0; Node *p = NULL; //char judge_str[102]; for(k = 0; k < Q; k++){ val = ELFHash(pQry[k]); h_val = val % hash_size; if( NULL == pHash[h_val] ){ pHash[h_val] = new Node; pHash[h_val]->val = val; pHash[h_val]->next = NULL; cnt++; }else{ p = pHash[h_val]; bool inserted = 0; while(p->next != NULL){ if(p->val == val){ // inserted. inserted = 1; break; } p = p->next; } // at the end of the list if(p->val == val){ // inserted. inserted = 1; } if(!inserted){ p->next = new Node; p->next -> val = val; p->next -> next = NULL; cnt++; } }//ELSE //judge if(cnt == S){ switch_cnt++; cnt = 0; //strcpy(pQry[k], judge_str); // free int j = 0; for(j = 0; j < hash_size; j++){ if(NULL == pHash[j]) continue; Node * tmp; p = pHash[j]; while(p!=NULL){ tmp = p->next; delete p; p = tmp; } pHash[j] = NULL; }//for /*k++; // next query for(; k < Q; k++){ if(strcmp(pQry[k], judge_str) == 0) break; } if( k < Q ) switch_cnt++; */ k--; }//if }//for //printf("Case #%d: %d/n", i+1, switch_cnt); out_f<<"Case #"<<i+1<<':'<<' '<<switch_cnt<<endl; } in_f.close(); out_f.close(); return; }