Lightoj 1428 Melody Comparison

Source: Internet
Author: User



At the beginning of the submission, I was surprised to find out.



Checked n times finally found strlen written in the For loop, so that the calculation of time complexity is n^2



Problem Solving Ideas:



Find the B string in the position where a string appears.



Then we calculate the height array separately for a string, and then enumerate each position to find the substring that can be generated, and then the height can be reduced.





#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define N 100005
char s [N];
int r [N], sa [N], height [N], rank [N], wa [N], wb [N], wv [N], ws [N];
int rmax [N];
int cmp (int * r, int a, int b, int l) {
return r [a] == r [b] && r [a + l] == r [b + l];
}
void da (int * r, int * sa, int n, int m) {
int i, j, p, * x = wa, * y = wb;
for (i = 0; i <m; i ++) ws [i] = 0;
for (i = 0; i <n; i ++) ws [x [i] = r [i]] ++;
for (i = 1; i <m; i ++) ws [i] + = ws [i-1];
for (i = n-1; i> = 0; i--) sa [-ws [x [i]]] = i;
for (j = 1, p = 1; p <n; m = p, j << = 1) {
for (i = n-j, p = 0; i <n; i ++) y [p ++] = i;
for (i = 0; i <n; i ++) if (sa [i]> = j) y [p ++] = sa [i] -j;
for (i = 0; i <n; i ++) wv [i] = x [y [i]];
for (i = 0; i <m; i ++) ws [i] = 0;
for (i = 0; i <n; i ++) ws [wv [i]] ++;
for (i = 1; i <m; i ++) ws [i] + = ws [i-1];
for (i = n-1; i> = 0; i--) sa [-ws [wv [i]]] = y [i];
swap (x, y);
for (p = 1, x [sa [0]] = 0, i = 1; i <n; i ++) x [sa [i]] = cmp (y, sa [i-1], sa [i], j)? p-1: p ++;
}
}
void calheight (int * r, int * sa, int n) {
int i, j, k = 0;
for (i = 1; i <= n; i ++) rank [sa [i]] = i;
for (i = 0; i <n; height [rank [i ++]] = k)
for (k? k-: 0, j = sa [rank [i] -1]; r [i + k] == r [j + k]; k ++);
// for (i = 1; i <= n; i ++) printf ("% d", height [i]);
}
int main () {
#ifndef ONLINE_JUDGE
freopen ("in.txt", "r", stdin);
#endif
int T;
scanf ("% d", & T);
int cas = 1;
while (T-) {
int l = 0;
scanf ("% s", s);
int n1 = strlen (s);
for (int i = 0; i <n1; i ++) r [l ++] = s [i];
r [l ++] = 129;
scanf ("% s", s);
int n2 = strlen (s);
for (int i = 0; i <n2; i ++) r [l ++] = s [i];
r [l] = 0;
da (r, sa, l + 1,130);
calheight (r, sa, l);
Hell
Hell
memset (rmax, -1, sizeof (rmax));
for (int i = rank [n1 + 1] +1; i <= l; i ++) {
if (height [i]> = n2) {
rmax [sa [i]] = n2-1;
}
else break;
}
for (int i = l; i> = 0; i-) {
if (rmax [i] ==-1) rmax [i] = rmax [i + 1] +1; // The initial value is set to -1 because the digits just start from -1 and the last few numbers also meet this formula
}
r [n1] = 0;
da (r, sa, n1 + 1,130);
calheight (r, sa, n1); // The first mistake was made because the height was not recalculated. The string placed after it affected the result.
// for (int i = 0; i <n1; i ++) printf ("% d:% d% d:% d \ n", i, rmax [i], i, height [rank [i]]);
long long ans = 0;
for (int i = 1; i <= n1; i ++) {// Subtract the number of repeated substrings of this suffix every time, because the height array is the same as the previous lcp, so there is exactly one that has not been calculated. Ask for
int total = min (rmax [sa [i]], n1-sa [i]);
if (total> height [i]) ans + = total-height [i];
}
printf ("Case% d:% lld \ n", cas ++, ans);
}
return 0;
}






Lightoj 1428 Melody Comparison


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.