BZOJ2255 : [Swerc2010]Palindromic DNA

考虑2-SAT建图,设$a[i][0..1]$表示$i$变不变,$b[i][0..1]$表示$i$是下降还是上升。

首先相邻的不能同时动,说明$a[i]$和$a[i+1]$里最多选一个。

对于$x$和$y$要相等,假设$s[x]\geq s[y]$。

$1.$若$s[x]-s[y]=3$,则视为$1$,并交换$x,y$。

$2.$若$s[x]=s[y]$,那么它们的任何行动都是相等的:

$a[x][0]\leftrightarrow a[y][0]$

$a[x][1]\leftrightarrow a[y][1]$

$b[x][0]\leftrightarrow b[y][0]$

$b[x][1]\leftrightarrow b[y][1]$

$3.$若$s[x]-s[y]=1$,那么它们有且仅能动一个,方向也是定的:

$a[x][0]\leftrightarrow a[y][1]$

$a[x][1]\leftrightarrow a[y][0]$

$a[x][1]$和$b[x][0]$最多只能选一个

$a[y][1]$和$b[y][1]$最多只能选一个

$4.$若$s[x]-s[y]=2$,那么它们都要动,而且方向相反:

$a[x][0]\rightarrow a[x][1]$

$a[y][0]\rightarrow a[y][1]$

$b[x][0]\leftrightarrow b[y][1]$

$b[x][1]\leftrightarrow b[x][0]$

求出SCC,若某个$a[i][0]$和$a[i][1]$在同一个SCC则无解,不需要考虑$b$,因为可以既不上升也不下降。

时间复杂度$O(n+m)$。

#include<cstdio>
#include<algorithm>
using namespace std;
const int N=100010,M=N*4,E=2000000;
int n,m,i,j,k,cnt,a[N][2],b[N][2],p[N],g[2][M],v[2][E],nxt[2][E],ed,q[M],t,f[M];
bool vis[M];char s[N];
inline void read(int&a){char c;while(!(((c=getchar())>=‘0‘)&&(c<=‘9‘)));a=c-‘0‘;while(((c=getchar())>=‘0‘)&&(c<=‘9‘))(a*=10)+=c-‘0‘;}
inline int getid(char x){
  if(x==‘A‘)return 0;
  if(x==‘G‘)return 1;
  if(x==‘T‘)return 2;
  return 3;
}
inline void add(int x,int y){
  v[0][++ed]=y;nxt[0][ed]=g[0][x];g[0][x]=ed;
  v[1][ed]=x;nxt[1][ed]=g[1][y];g[1][y]=ed;
}
inline void add2(int x,int y){add(x,y),add(y,x);}
inline void check(int x,int y){
  if(s[x]<s[y])swap(x,y);
  int w=s[x]-s[y];
  if(w==3)w=1,swap(x,y);
  if(!w){
    add2(a[x][0],a[y][0]);
    add2(a[x][1],a[y][1]);
    add2(b[x][0],b[y][0]);
    add2(b[x][1],b[y][1]);
    return;
  }
  if(w==1){
    add2(a[x][0],a[y][1]);
    add2(a[x][1],a[y][0]);
    add(a[x][1],b[x][0]);
    add(b[x][1],a[x][0]);
    add(a[y][1],b[y][1]);
    add(b[y][0],a[x][0]);
    return;
  }
  add(a[x][0],a[x][1]);
  add(a[y][0],a[y][1]);
  add2(b[x][0],b[y][1]);
  add2(b[x][1],b[y][0]);
}
void dfs1(int x){
  vis[x]=1;
  for(int i=g[0][x];i;i=nxt[0][i])if(!vis[v[0][i]])dfs1(v[0][i]);
  q[++t]=x;
}
void dfs2(int x,int y){
  vis[x]=0;f[x]=y;
  for(int i=g[1][x];i;i=nxt[1][i])if(vis[v[1][i]])dfs2(v[1][i],y);
}
int main(){
  while(~scanf("%d%d",&n,&m)){
    if(!n)return 0;
    scanf("%s",s);
    for(i=0;i<n;i++)s[i]=getid(s[i]);
    for(cnt=i=0;i<n;i++)for(j=0;j<2;j++)a[i][j]=++cnt,b[i][j]=++cnt;
    for(ed=0,i=1;i<=cnt;i++)g[0][i]=g[1][i]=0;
    for(i=0;i<n;i++){
      if(i)add(a[i][1],a[i-1][0]);
      if(i+1<n)add(a[i][1],a[i+1][0]);
    }
    while(m--){
      read(k);
      for(i=0;i<k;i++)read(p[i]);
      for(i=0,j=k-1;i<j;i++,j--)check(p[i],p[j]);
    }
    for(t=0,i=1;i<=cnt;i++)if(!vis[i])dfs1(i);
    for(i=cnt;i;i--)if(vis[q[i]])dfs2(q[i],q[i]);
    for(i=0;i<n;i++)if(f[a[i][0]]==f[a[i][1]])break;
    puts(i<n?"NO":"YES");
  }
}

  

时间: 2024-08-26 23:03:05

BZOJ2255 : [Swerc2010]Palindromic DNA的相关文章

UVA - 1368 DNA Consensus String

DNA Consensus String Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description  Figure 1. DNA (Deoxyribonucleic Acid) is the molecule which contains the genetic instructions. It consists of four different nuc

[LeetCode]Repeated DNA Sequences

题目:Repeated DNA Sequences 给定包含A.C.G.T四个字符的字符串找出其中十个字符的重复子串. 思路: 首先,string中只有ACGT四个字符,因此可以将string看成是1,3,7,20这三个数字的组合串: 并且可以发现{ACGT}%5={1,3,2,0};于是可以用两个位就能表示上面的四个字符: 同时,一个子序列有10个字符,一共需要20bit,即int型数据类型就能表示一个子序列: 这样可以使用计数排序的思想来统计重复子序列: 这个思路时间复杂度只有O(n),但是

POJ2778 DNA Sequence Trie+矩阵乘法

题意:给定N个有A C G T组成的字符串,求长度为L的仅由A C G T组成的字符串中有多少个是不含给定的N个字符串的题解: 首先我们把所有的模式串(给定的DNA序列)建Trie,假定我们有一个匹配串,并且在匹配过程到S[i]这个字符时匹配到了Trie上的某个节点t,那么有两种可能: 匹配失败:t->child[S[i]]为空,跳转到t->fail,因此t->fail一定不能是某个模式串的结尾: 匹配成功:跳转到t->child[S[i+1]],因此t->child[S[i

[string]Longest Palindromic Substring

Total Accepted: 82026 Total Submissions: 379898 Difficulty: Medium Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. Subscrib

LeetCode OJ:Longest Palindromic Substring(最长的回文字串)

Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 玩了两天dota2,罪过罪过,还是应该老老实实刷题啊. 题目求得是最长的回文子串,这里使用的是暴力的解法,也就是解决两种回文"asdsa"以

USACO 1.2 Palindromic Squares

Palindromic SquaresRob Kolstad Palindromes are numbers that read the same forwards as backwards. The number 12321 is a typical palindrome. Given a number base B (2 <= B <= 20 base 10), print all the integers N (1 <= N <= 300 base 10) such that

CodeForces 520C DNA Alignment

题意: 一段DNA序列(10^5长度)  定义h函数为两序列相同碱基个数  p函数为分别移动两个DNA序列后所有可能的h函数之和  问使p最大的序列有多少个 思路: 根据p函数的定义  我们发现p这个函数其实就是A序列每个碱基和B序列每个碱基比较再乘一个n 因此可以贪心构造B序列  即每次新加一个碱基必定是A序列中出现次数最多的碱基 那么最后的答案就是A序列中出现次数最多的碱基种类数的n次方 代码: #include<cstdio> #include<iostream> #incl

HDU - 1560 DNA sequence

给你最多8个长度不超过5的DNA系列,求一个包含所有系列的最短系列. 迭代加深的经典题.(虽然自己第一次写) 定一个长度搜下去,搜不出答案就加深大搜的限制,然后中间加一些玄学的减枝 //Twenty #include<cstdio> #include<cstdlib> #include<iostream> #include<algorithm> #include<cmath> #include<cstring> #include<

codechef AUG17 T4 Palindromic Game

Palindromic Game Problem Code: PALINGAM There are two players A, B playing a game. Player A has a string s with him, and player B has string t with him. Both s and t consist only of lower case English letters and are of equal length. A makes the firs