zoukankan      html  css  js  c++  java
  • UVa 12388 Anti-Rhyme Pairs

    方法:hash 二分

    最优化问题转化为判定性问题。先预处理每个string的前缀hash,然后对于每组query,二分答案。当两个prefix 的hash value相同的时候,稳妥的方法是用O(prefix.length())的方法检查这两个prefix是否相同(1.430s); 本题采用 bkdr hash,如果当hash value相同就认定两个prefix 相同,也通过了testcase(0.290s)。

    code:

      1 #include <cstdio>
      2 #include <cstring>
      3 #include <algorithm>
      4 #include <iostream>
      5 #include <string>
      6 #include <vector>
      7 #include <stack>
      8 #include <bitset>
      9 #include <cstdlib>
     10 #include <cmath>
     11 #include <set>
     12 #include <list>
     13 #include <deque>
     14 #include <map>
     15 #include <queue>
     16 #include <fstream>
     17 #include <cassert>
     18 #include <unordered_map>
     19 #include <unordered_set>
     20 #include <cmath>
     21 #include <sstream>
     22 #include <time.h>
     23 #include <complex>
     24 #include <iomanip>
     25 #define Max(a,b) ((a)>(b)?(a):(b))
     26 #define Min(a,b) ((a)<(b)?(a):(b))
     27 #define FOR(a,b,c) for (ll (a)=(b);(a)<(c);++(a))
     28 #define FORN(a,b,c) for (ll (a)=(b);(a)<=(c);++(a))
     29 #define DFOR(a,b,c) for (ll (a)=(b);(a)>=(c);--(a))
     30 #define FORSQ(a,b,c) for (ll (a)=(b);(a)*(a)<=(c);++(a))
     31 #define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
     32 #define FOREACH(a,b) for (auto &(a) : (b))
     33 #define rep(i,n) FOR(i,0,n)
     34 #define repn(i,n) FORN(i,1,n)
     35 #define drep(i,n) DFOR(i,n-1,0)
     36 #define drepn(i,n) DFOR(i,n,1)
     37 #define MAX(a,b) a = Max(a,b)
     38 #define MIN(a,b) a = Min(a,b)
     39 #define SQR(x) ((LL)(x) * (x))
     40 #define Reset(a,b) memset(a,b,sizeof(a))
     41 #define fi first
     42 #define se second
     43 #define mp make_pair
     44 #define pb push_back
     45 #define all(v) v.begin(),v.end()
     46 #define ALLA(arr,sz) arr,arr+sz
     47 #define SIZE(v) (int)v.size()
     48 #define SORT(v) sort(all(v))
     49 #define REVERSE(v) reverse(ALL(v))
     50 #define SORTA(arr,sz) sort(ALLA(arr,sz))
     51 #define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
     52 #define PERMUTE next_permutation
     53 #define TC(t) while(t--)
     54 #define forever for(;;)
     55 #define PINF 1000000000000
     56 #define newline '
    '
     57 
     58 #define test if(1)if(0)cerr
     59 using namespace std;
     60 using namespace std;
     61 typedef vector<int> vi;
     62 typedef vector<vi> vvi;
     63 typedef pair<int,int> ii;
     64 typedef pair<double,double> dd;
     65 typedef pair<char,char> cc;
     66 typedef vector<ii> vii;
     67 typedef long long ll;
     68 typedef unsigned long long ull;
     69 typedef pair<ll, ll> l4;
     70 const double pi = acos(-1.0);
     71 
     72 const int maxn = 1e5;
     73 string str[maxn];
     74 vector<ull> hashstr[maxn];
     75 int n, q;
     76 
     77 bool valid(int a, int b, int mid)
     78 {
     79     --a, --b, --mid;
     80     // to be safe, one should perform the substring test on the following line.
     81     return  (hashstr[a][mid] == hashstr[b][mid]);// && str[a].substr(0, mid+1)==str[b].substr(0,mid+1));
     82 }
     83 int main()
     84 {
     85     ios::sync_with_stdio(false);
     86     cin.tie(0);
     87     int T;  cin >> T;
     88     repn(kase, T)
     89     {
     90         cin >> n;
     91         rep(i, n) cin >> str[i];
     92         rep(i, n)
     93         {
     94             hashstr[i].clear();
     95             ull cur = 0, len = str[i].length();
     96             rep(j, len)
     97             {
     98                 cur = 131*cur + (ull)str[i][j];
     99                 hashstr[i].pb(cur);
    100             }
    101         }
    102         cout << "Case " << kase << ":
    ";
    103         cin >> q;
    104         rep(i, q)
    105         {
    106             int x, y;   cin >> x >> y;
    107             int left = 1, right = min(str[x-1].length(), str[y-1].length()), mid, ans=0;
    108             while (left <= right)
    109             {
    110                 mid = (left+right)>>1;
    111                 if (valid(x,y,mid))
    112                 {
    113                     ans = mid;
    114                     left = mid+1;
    115                 }
    116                 else
    117                     right = mid-1;
    118             }
    119             cout << ans << newline;
    120         }
    121     }
    122 }
    123 /*
    124  
    125  2
    126  5
    127  daffodilpacm daffodiliupc distancevector distancefinder distinctsubsequence 4
    128  1 2
    129  1 5
    130  3 4
    131  4 5
    132  2
    133  acm
    134  icpc
    135  2
    136  1 2
    137  2 2
    138 
    139 */
    View Code
  • 相关阅读:
    hadoop中namenode发生故障的处理方法
    开启虚拟机所报的错误:VMware Workstation cannot connect to the virtual machine. Make sure you have rights to run the program, access all directories the program uses, and access all directories for temporary fil
    Hbase的安装与部署(集群版)
    分别用反射、编程接口的方式创建DataFrame
    用Mapreduce求共同好友
    SparkSteaming中直连与receiver两种方式的区别
    privot函数使用
    Ajax无刷新显示
    使用ScriptManager服务器控件前后台数据交互
    数据库知识
  • 原文地址:https://www.cnblogs.com/skyette/p/6358865.html
Copyright © 2011-2022 走看看