Suffix array LCP-- template title

Meaning of the questions:

S and T you string string string with all prefixes T to match the string S (matching value is the longest common substring).

Ask your total sum is.

Ideas:

First two S, T string upside down, and then stitching S # T synthesis bunch, running about suffix array

Calculating the length of the longest match each T suffix in sorted in rank. (Two for the front and rear can)

Finally dp suffixes take the max, the cumulative answer. (Because the suffix from the start pos ANS1 is certainly suffix contains ans2 pos-1 from the beginning, so if ans2 <ans1, it should take max (ans1) of ANS2

  1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
  2 #include <cstdio>//sprintf islower isupper
  3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
  4 #include <iostream>//pair
  5 #include <fstream>//freopen("C:\\Users\\13606\\Desktop\\草稿.txt","r",stdin);
  6 #include <bitset>
  7 //#include <map>
  8 //#include<unordered_map>   https://www.nitacm.com/problem_show.php?pid=585
  9 #include <vector>
 10 #include <stack>
 11 #include <set>
 12 #include <string.h>//strstr substr
 13 #include <string>
 14 #include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
 15 #include <cmath>
 16 #include <deque>
 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
 18 #include <vector>//emplace_back
 19 //#include <math.h>
 20 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
 21 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
 22 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
 23 #define fo(a,b,c) for(register int a=b;a<=c;++a)
 24 #define fr(a,b,c) for(register int a=b;a>=c;--a)
 25 #define mem(a,b) memset(a,b,sizeof(a))
 26 #define pr printf
 27 #define sc scanf
 28 #define ls rt<<1
 29 #define rs rt<<1|1
 30 typedef long long ll;
 31 #define rint register int
 32 void swapp(int &a,int &b);
 33 double fabss(double a);
 34 int maxx(int a,int b);
 35 int minn(int a,int b);
 36 int Del_bit_1(int n);
 37 int lowbit(int n);
 38 int abss(int a);
 39 //const long long INF=(1LL<<60);
 40 const double E=2.718281828;
 41 const double PI=acos(-1.0);
 42 const int inf=(1<<30);
 43 const double ESP=1e-9;
 44 const int mod=(int)1e9+7;
 45 const int N=(int) 2E6 + 10 ;
 46 is  void PR ( int _ [], int n-)
 47  {
 48      for ( int I = . 1 ; I <= n-; ++ I)
 49          PR ( " % D " , _ [I]);
 50      PR ( " \ n- " );
 51 is  }
 52 is  char S [N], S1 [N], S2 [N];
 53 is  int A [N];
 54 is  int SA [N], RK [N], S_A [N] , T [N]; // bucket size to be N, the discharge is because Rank; 
55  int height [N], H [N];// H is the length of position i, heidgt length of ranki; 
56 is  void the Init ( int _ [], int n-)
 57 is  {
 58      for (RINT i = 0 ; i <= n-; ++ i)
 59          _ [i ] = 0 ;
 60  }
 61 is  BOOL CMP ( int I, int J, int K)
 62 is  {
 63 is      return S_A [I] == S_A [J] && S_A [I + K] == S_A [J + K];
 64  }
 65  void the Sort ( int len)
 66  {
 67     int m=30;//字符集大小;
 68     for(rint i=1;i<=len;++i) ++t[a[i]],rk[i]=a[i];
 69     for(rint i=1;i<=m;++i) t[i]+=t[i-1];
 70     for(rint i=len;i>=1;--i) sa[t[rk[i]]--]=i;
 71     for(rint k=1;k<=len;k<<=1)
 72     {
 73         int cnt=0;
 74         //按第二个rank排;
 75         for(rint i=len-k+1;i<=len;++i) s_a[++cnt]=i;
 76         for(rint i=1;i<=len;++i)if(sa[i]>k) s_a[++cnt]=sa[i]-k;
 77         //按第一个rank排;
 78         Init(t,m);
 79         for(rint i=1;i<=len;++i) ++t[rk[s_a[i]]];
 80         for(rint i=1;i<=m;++i) t[i]+=t[i-1];
 81         for(rint i=len;i>=1;--i) sa[t[rk[s_a[i]]]--]=s_a[i];
 82 
 83         swap(rk,s_a);rk[sa[1]]=cnt=1;
 84         for(rint i=2;i<=len;++i)
 85             rk[sa[i]]=cmp(sa[i],sa[i-1],k)?cnt:++cnt;
 86         if(cnt==len)break;
 87         m=cnt;
 88     }
 89     //求height数组;
 90     for(rint i=1;i<=len;++i)
 91     {
 92         h[i]=max(0,h[i-1]-1);
 93         if(rk[i]==1)continue;
 94         while(a[i+h[i]]==a[sa[rk[i]-1]+h[i]]) ++h[i];
 95     }
 96     for(rint i=1;i<=len;++i) height[i]=h[sa[i]];
 97 }
 98 
 99 int ans[N],res[N];
100 
101 int main()
102 {
103 //    freopen("D:\\Chrome Download\\testdata (2).in","r",stdin);
104     int len1,len2;
105     sc("%d%d",&len1,&len2);
106     sc("%s%s",s1+1,s2+1);
107     int len=0;
108     for(int i=len1;i>=1;--i)
109         a[++len]=s1[i]-'a'+1;
110     a[++len]=27;
111     for(int i=len2;i>=1;--i)
112         a[++len]=s2[i]-'a'+1;
113     Sort(len);
114 //    PR(height,len);
115 //    PR(sa,len);
116 //    PR(rk,len);
117     int temp=0;
118     for(int i=1;i<=len;++i)
119     {
120     //    temp=min(temp,height[i+1]);
121         if(sa[i]<=len1)
122             temp=height[i+1];
123         else
124             ans[i]=temp,temp=min(temp,height[i+1]);
125     }
126     temp=0;
127     for(int i=len;i>=1;--i)
128     {
129     //    temp=min(temp,height[i+1]);
130         if(sa[i]<=len1)
131             temp=height[i];
132         else
133             ans[i]=max(ans[i],temp),temp=min(temp,height[i]);
134     }
135 //    PR(ans,len);
136     ll Ans=0;
137     for(int i=len;i>=len1+2;--i)
138     {
139         res[i]=max(res[i+1],ans[rk[i]]);
140         Ans+=res[i];
141     }
142     pr("%lld\n",Ans);
143     return 0;
144 }
145 
146 /**************************************************************************************/
147 
148 int maxx(int a,int b)
149 {
150     return a>b?a:b;
151 }
152 
153 void swapp(int &a,int &b)
154 {
155     a^=b^=a^=b;
156 }
157 
158 int lowbit(int n)
159 {
160     return n&(-n);
161 }
162 
163 int Del_bit_1(int n)
164 {
165     return n&(n-1);
166 }
167 
168 int abss(int a)
169 {
170     return a>0?a:-a;
171 }
172 
173 double fabss(double a)
174 {
175     return a>0?a:-a;
176 }
177 
178 int minn(int a,int b)
179 {
180     return a<b?a:b;
181 }

 

Guess you like

Origin www.cnblogs.com/--HPY-7m/p/11562951.html