Advertisement
pseudocreator

go

Mar 28th, 2014
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.81 KB | None | 0 0
  1. //sort check..
  2. #include <cstdlib>
  3. #include <cstdio>
  4. #include <algorithm>
  5. #include <ctime>
  6.  
  7. using namespace std;
  8.  
  9. const int MAXN = 1000;
  10. const int MAXL = 9;
  11. const long long FH_PRIME = 426389;
  12.  
  13. int n,l,m;
  14. unsigned long long train[MAXN][MAXL];
  15. unsigned long long t_fast_hash[MAXN];
  16. unsigned long long t_additional_hash[MAXN];
  17. unsigned int t_third_hash[MAXN];
  18. unsigned long long p_fast_hash[MAXL];
  19. int order[MAXN];
  20. int same[MAXN];
  21.  
  22. inline void fast_hash(int train_id){
  23.   t_fast_hash[train_id] = 0;
  24.   for (int i = 0; i<MAXL; i++){
  25.     t_fast_hash[train_id] += train[train_id][i]*p_fast_hash[i];
  26.   }
  27. }
  28.  
  29. inline void third_hash(int train_id){
  30.   unsigned char *t;
  31.   t = (unsigned char *)train[train_id];
  32.   t_third_hash[train_id] = 0;
  33.   for(int i = 0; i < MAXL*8; i++){
  34.     t_third_hash[train_id] += t[i];
  35.     t_third_hash[train_id] += (t_third_hash[train_id] << 10);
  36.     t_third_hash[train_id] ^= (t_third_hash[train_id] >> 6);
  37.   }
  38.   t_third_hash[train_id] += (t_third_hash[train_id] << 3);
  39.   t_third_hash[train_id] ^= (t_third_hash[train_id] >> 11);
  40.   t_third_hash[train_id] += (t_third_hash[train_id] << 15);
  41. }
  42.  
  43. inline void additional_hash(int train_id){
  44.   t_additional_hash[train_id] = 0;
  45.   for (int i = 0; i<MAXL; i++){
  46.     t_additional_hash[train_id] ^= (((train[train_id][i] >> 43) * 7561 + (train[train_id][i] >> 22)) * 1597 + train[train_id][i]);
  47.   }
  48. }
  49.  
  50. inline void set_train_pos(int train_id, int pos, long long newc){
  51.   int num = pos/12;
  52.   pos = pos % 12;
  53.   long long t = ((train[train_id][num] >> (5*pos))&31)^newc;
  54.   train[train_id][num] = (train[train_id][num] ^ (t << (5*pos)));
  55. }
  56.  
  57. inline int get_train_pos(int train_id, int pos){
  58.   int num = pos/12;
  59.   pos = pos % 12;
  60.   return (train[train_id][num] >> (5*pos))&31;
  61. }
  62.  
  63. inline bool sort_func(int i, int j){
  64.   return ((t_fast_hash[i]<t_fast_hash[j]) || ((t_fast_hash[i] == t_fast_hash[j])&&(t_additional_hash[i]<t_additional_hash[j])));
  65. }
  66.  
  67. inline void super_mega_ultra_fast_check(int lpos){
  68.   for (int i = 0; i<MAXL; i++){
  69.     if (train[order[lpos]][i] != train[order[lpos+1]][i]){
  70.       return;
  71.     }
  72.   }
  73.   if (same[order[lpos]] < 2) same[order[lpos]] = 2;
  74.   if (same[order[lpos+1]] < 2) same[order[lpos+1]] = 2;
  75. }
  76.  
  77. inline bool next_sort_func(int i, int j){
  78.   return (t_third_hash[i] < t_third_hash[j]);
  79. }
  80.  
  81. inline void check_third_hash(int lpos, int rpos){
  82.   sort(&order[lpos],&order[rpos],next_sort_func);
  83.   int left = lpos;
  84.   lpos++;
  85.   int counter = 1;
  86.   while (lpos < rpos){
  87.     if (t_third_hash[order[lpos]] == t_third_hash[order[lpos-1]]){
  88.       counter++;
  89.     }else{
  90.       for (int i = left; i<lpos; i++){
  91.         if (same[order[i]] < counter) same[order[i]] = counter;
  92.       }
  93.       counter = 1;
  94.       left = lpos;
  95.     }
  96.     lpos++;
  97.   }
  98.   for (int i = left; i<lpos; i++){
  99.     if (same[order[i]] < counter) same[order[i]] = counter;
  100.   }
  101. }
  102.  
  103. inline void update(){
  104.   sort(&order[0],&order[n],sort_func);
  105.   int left = 0;
  106.   int counter = 1;
  107.   int pos = 1;
  108.   while (pos < n){
  109.     if (t_fast_hash[order[pos]] == t_fast_hash[order[pos-1]] && t_additional_hash[order[pos]] == t_additional_hash[order[pos-1]]){
  110.       counter++;
  111.     }else{
  112.       if (counter <= 2){
  113.         if (counter == 2){
  114.           super_mega_ultra_fast_check(left);
  115.         }
  116.       }else{
  117.         check_third_hash(left,pos);
  118.       }
  119.       counter = 1;
  120.       left = pos;
  121.     }
  122.     pos++;
  123.   }
  124.   if (counter <= 2){
  125.     if (counter == 2){
  126.       super_mega_ultra_fast_check(left);
  127.     }
  128.     }else{
  129.     check_third_hash(left,pos);
  130.   }
  131. }
  132.  
  133. int main(int argc, char** argv) {
  134.     time_t now = clock();
  135.     freopen("trains.in","r",stdin);
  136.     freopen("trains.out","w",stdout);
  137.     scanf("%d %d %d\n",&n,&l,&m);
  138.     char c;
  139.     for (int i = 0; i<n; i++){
  140.       order[i] = i;
  141.       same[i] = 1;
  142.       for (int j = 0; j<l; j++){
  143.         c = getchar();
  144.         set_train_pos(i,j,c-'a');
  145.       }
  146.       c = getchar();
  147.     }
  148.     p_fast_hash[0] = 1;
  149.     for (int i = 1; i<MAXL; i++) p_fast_hash[i] = p_fast_hash[i]*FH_PRIME;
  150.     for (int i = 0; i<n; i++){
  151.       fast_hash(i);
  152.       additional_hash(i);
  153.       third_hash(i);
  154.     }
  155.     update();
  156.     for (int i = 0; i<m; i++){
  157.       int n1,n2,p1,p2;
  158.       scanf("%d %d %d %d\n",&n1,&p1,&n2,&p2);
  159.       n1--;
  160.       n2--;
  161.       p1--;
  162.       p2--;
  163.       int t1 = get_train_pos(n1,p1);
  164.       int t2 = get_train_pos(n2,p2);
  165.       set_train_pos(n1,p1,t2);
  166.       set_train_pos(n2,p2,t1);
  167.       fast_hash(n1);
  168.       fast_hash(n2);
  169.       additional_hash(n1);
  170.       additional_hash(n2);
  171.       third_hash(n1);
  172.       third_hash(n2);
  173.       update();
  174.       if ((float)(clock()-now)/CLOCKS_PER_SEC > 2.8){
  175.         while(i<m){
  176.           scanf("%d %d %d %d\n",&n1,&p1,&n2,&p2);
  177.           i++;
  178.         }
  179.         break;
  180.       }
  181.     }
  182.     for (int i = 0; i<n; i++) printf("%d\n",same[i]);
  183.     return 0;
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement