Advertisement
Guest User

Untitled

a guest
Nov 16th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int MX=5009;
  5. const ll inf=(1ll<<61);
  6. const int mod1=1e9+7;
  7. const int mod2=1e9+9;
  8. const int p1=29;
  9. const int p2=37;
  10. map<string,int>h1[MX],h2[MX];
  11. void buildhash(string s){
  12. //    for(int i=0;i<s.size();i++)s[i]-='a';
  13.     h1[0][s]=s[0];
  14.     h2[0][s]=s[0];
  15.     for(int i=1;i<s.size();i++){
  16.         h1[i][s]=(h1[i-1][s]*p1+s[i])%mod1;
  17.         h2[i][s]=(h2[i-1][s]*p2+s[i])%mod2;
  18.     }
  19. }
  20. bool bn(string s,string s1){
  21.     int l=0,r=s.size()-1;
  22.     int ans=0;
  23.     while(l<=r){
  24.         int mid=(l+r)/2;
  25.         if(h1[mid][s]==h1[mid][s1]&&h2[mid][s]==h2[mid][s1]){
  26.             l=mid+1;
  27.             ans=mid+1;
  28.         }
  29.         else{
  30.             r=mid-1;
  31.             ans=mid;
  32.         }
  33.     }
  34.     if(ans==s.size())return 0;
  35.     return s[ans]<s1[ans];
  36. }
  37. bool cmp(string a,string b){
  38.     if(a.size()!=b.size())return a.size()<b.size();
  39.     return bn(a,b);
  40. }
  41. string s1="asas";
  42. string s2="asas";
  43. int main(){
  44.     buildhash(s1);
  45.     buildhash(s2);
  46.     cout<<bn(s1,s2)<<endl;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement