POJ 3080 Blue Jeans

解题思路:将所有串链接在一起,中间用不同的分隔符分割,求解后缀数组Height,二分求解连续长度>=k的公共子串是否包含了所有情况。

代码
#include<iostream>
using namespace std;

#define MAX_LEN 1000

int wa[MAX_LEN], wb[MAX_LEN], wv[MAX_LEN], wd[MAX_LEN], Height[MAX_LEN], sa[MAX_LEN], rank[MAX_LEN];
int n;

inline
bool IsEqual(int *r, int a, int b, int l)
{
return (r[a] == r[b] && r[a + l] == r[b + l]);
}

void da(int *r, int m)
{
int i, j, p, *x = wa, *y = wb, *t;
memset(wd,
0, sizeof(wd));
for (i = 0; i < n; i++) wd[x[i] = r[i]]++; x[n] = y[n] = 0;
for (i = 1; i < m; i++) wd[i] += wd[i - 1];
for (i = n - 1; i >= 0; i--) sa[--wd[x[i]]] = i;

for (p = 1, j = 1; p <= n; m = p, j *= 2)
{
for(p = 0, i = n - j; 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]];
memset(wd,
0, sizeof(wd));
for(i = 0; i < n; i++) wd[wv[i]]++;
for(i = 1; i < m; i++) wd[i] += wd[i - 1];
for(i = n - 1; i >= 0; i--) sa[--wd[wv[i]]] = y[i];
for(t = x, x = y, y = t, i = 1, p = 2,x[sa[0]] = 1; i < n; i++)
x[sa[i]]
= IsEqual(y, sa[i-1], sa[i], j) ? p - 1 : p++;
}
}

void CalHeight(int *r)
{
int i, j, k;
for (i = 0; i < n; i++)rank[sa[i]] = i;
for (i = 0, Height[0] = k = 0; i < n; Height[rank[i++]] = k)
for (k?k--:0, j=(rank[i]>0)?sa[rank[i]-1]:0; rank[i]>0&&r[i+k]==r[j+k]; k++);
}

int main()
{
int m, t, r[700], i, j, sp, sb, se, mid, s, l, ans, c, w;
char str[61];
bool visit[10], isfind;
scanf(
"%d", &w);
while (w--)
{
scanf(
"%d", &m); sp = 5;
for(j = n = 0; j < m; j++)
{
scanf(
"%s", str);
for (i = 0; i < 60; i++)
{
if(str[i] == 'A')r[n++] = 1;
else if(str[i] == 'T')r[n++] = 2;
else if(str[i] == 'G')r[n++] = 3;
else r[n++] = 4;
}
r[n
++] = sp++;
}
da(r, sp), CalHeight(r);
for(sb=3,se=60,mid=31,s=0,ans=-1; sb < se;)
{
isfind
= false;
for(int i = 0; (i < n) && !isfind ; i++)
{
if(Height[i] < mid)memset(visit, false, sizeof(visit)), t = 0;
if(Height[i] >= mid)
{
if(t == 0){
l
= sa[i-1] / 61;
if(!visit[l])visit[l] = true, t++;
}
l
=sa[i]/61;
if(!visit[l])visit[l] = true, t++;
if(t == m)isfind = true,ans = i;
}
}
if(isfind && mid == se)break;
if(isfind)(sb == mid && se > sb) ? (mid = se):(sb = mid);
else se = mid - 1, mid = (se + sb) / 2;
}
if(ans != -1)
for(int i = 0; i < mid; i++)
{
c
= r[sa[ans] + i];
if(c == 1)printf("A");
else if(c == 2)printf("T");
else if(c == 3)printf("G");
else printf("C");
}
else printf("no significant commonalities");
printf(
"\n");
}
return 0;
}

 

posted on 2010-11-30 09:42  ltang  阅读(532)  评论(0编辑  收藏  举报

导航