POJ 3415 不小於k的公共子串的個數

2022-08-19 08:48:10 字數 2215 閱讀 2929

common substrings

time limit:5000ms

memory limit:65536k

total submissions:9248

accepted:3071

description

a substring of a string t is defined as:

t(i, k)=titi

+1...ti+k

-1, 1≤i≤i+k-1≤|t|.

given two strings a, b and one integer k, we define s, a set of triples (i, j, k):

s = .

you are to give the value of |s| for specific a, b and k.

input

the input file contains several blocks of data. for each block, the first line contains one integer k, followed by two lines containing strings a and b, respectively. the input file is ended byk=0.

1 ≤ |a|, |b| ≤ 105

1 ≤ k ≤ min

characters of a and b are all latin letters.

output

for each case, output an integer |s|.

/*

poj 3415 不小於k的公共子串的個數(思路)

給你兩個子串求長度不小於k的公共子串的個數

因為每次列舉乙個子串就要和前面所有另乙個串的所有已經出現情況取乙個最小值

如果每次都把所有的掃瞄一遍的話很浪費時間,而且是與前面的所有取min,所有用棧儲存的話

棧頂的肯定是最大值,所以棧內元素就成單調增的了。 每次只需要更新比當前值大的情況就好了

從頭到尾列舉height,如果當前是屬於a串,則加上前面所有屬於b串的height-k+1.對於b串同理.

兩個串之間的公共字首是它們之間所有的最小值,所以用棧維護一下,保證棧裡是單調遞增的,

這樣對於新增的串只需要處理其中height大於它的一部分即可

hhh-2016-03-15 23:25:42

*/#include #include #include #include #include #include #include #include #include #define lson (i<<1)

#define rson ((i<<1)|1)

using namespace std;

typedef long long ll;

const int maxn = 200050;

int t1[maxn],t2[maxn],c[maxn];

bool cmp(int *r,int a,int b,int l)

void get_sa(int str,int sa,int rank,int height,int n,int m)

int k = 0;

n--;

for(int i = 0; i <= n; i++)

rank[sa[i]] = i;

for(int i = 0; i < n; i++)

}int rank[maxn];

int sa[maxn];

int str[maxn],mark[4],height[maxn];

char s1[maxn],s2[maxn];

ll num[4],ans[maxn];

ll solve(int len,int n,int k)

ans[++top] = height[i]-k+1;

if(sa[i-1]len) mark[top] = 2;

num[mark[top]] += height[i]-k+1;

if(sa[i] < len) sum += num[2];

if(sa[i] > len) sum += num[1];}}

return sum;

}int main()

return 0;

}

POJ 3294 不小於k個字串中的最長子串

將n個字串連線起來,中間用沒有出現過的字元隔開,然後求字尾陣列,二分答案,進行分組,判斷每組的字尾是否出現在不小於k的原串中。include include includeconst int maxn 1e5 1e3 int r maxn r 陣列儲存了字串中的每個元素值,除最後乙個元素外,每個元素...

lintcode 具有K個不同字元的子串

給定字串s和整數k.計算長度為k且包含k個不同字元的子串數 string abcabc k 3 answer 3 substrings abc bca cab string abacab k 3 answer 2 substrings bac cab 維護乙個陣列temp和乙個值cnt,記錄當前迭代...

lintcode 具有K個不同字元的子串

給定字串s和整數k.計算長度為k且包含k個不同字元的子串數 string abcabc k 3 answer 3 substrings abc bca cab string abacab k 3 answer 2 substrings bac cab 維護乙個陣列temp和乙個值cnt,記錄當前迭代...