ZOJ-1984 Genetic Code(搜索)

ZOJ Problem Set - 1984
Genetic Code
Time Limit: 5 Seconds      Memory Limit: 32768 KB      Special Judge
The connections between mathematics and biology are complicated. Most of the time they do not run along nice-looking links that merrily join at first glance, but they are abstract and not always easily established.

Lake Vostok - about 14000 square kilometers large, up to 650 meters deep, and covered by 3743 meters of ice - was recently discovered on the Antarctic continent. The lake remained under conditions of high pressure and no sunlight for several millions of years. It is believed that ordinary life has evolved to a more efficient form using a genetic code composed of only three bases (the current state of ignorance proclaims the four bases adenine, cytosine, guanine, and thymine). Until reasonable names are found, the three bases will be abbreviated as N, O, and P.

Moreover, the genome is single-stranded and directed, i.e., we may see it as a sequence over the alphabet {N,O,P}. Unless risking instability, it is necessary that the genome is a Thue-sequence, due to the Norwegian mathematician A. Thue (1863-1922). Define a subsegment of a sequence to be a connected subsequence, and call two subsegments adjacent if one follows immediately after the other in the sequence. A Thue-sequence is a sequence where no adjacent subsegments are equal. For example, NOPNO is and NOPNPNO is not a Thue-sequence, so that the first may be a genome whereas the second may not.

To be able to simulate experiments with the new genomes, you are asked to generate genomes of certain lengths.


Input

The input contains several test cases. Each test case consists of an integer n. You may assume that 1<=n<=5000. The last test case is followed by a zero.


Output

For each test case specified by n output on a single line any genome of length n. If no genome of length n exists, output a blank line instead.


Sample Input

1
2
10
20
0


Sample Output

N
NO
NONPNOPNPO
NONPNOPNPONOPNONPNOP

 

这道题还是搜索,不过使用递归的话会因为递归层数太多而溢出,所以改为非递归方式

注释掉的代码段是递归算法


#include <iostream>
using namespace std;
int seq[5002];
int n,len;
char ge[3]={'N','O','P'};

//bool found;
int ok(int x)
{
 int i,j,k;
 bool f;
 seq[len]=x;
 for(i=len-1,j=len-1,k=len; i>=0; i-=2,--j)
 {
  f=false;
  for(int a=i,b=j+1; a<=j,b<=k;++a,++b)
   if(seq[a]!=seq[b])
   {
    f=true;
    break;
   }
  if(!f)
  { 
   seq[len]=0;
   return 0;
  }
 }
 seq[len]=0;
 return 1;
}
/*void dfs(int i)
{
 if(!found)
  if(i==n)
  {
   found=true;
   for(int k=0;k<len;++k)
    cout<<ge[seq[k]-1]<<" ";
   cout<<endl;
   return;
  }
  else
  {
   for(int j=1;j<=3;++j)
   {
    if(ok(j))
    {
     seq[i]=j;
     len++;
     dfs(i+1);
     len--;
    }
   }
  }
}*/
void dfs(int i)
{
 int j=1;
 while(1)
 {
  if(i==n)
  {
   for(int k=0;k<len;++k)
     cout<<ge[seq[k]-1];
    cout<<endl;
   break;
  }
  else
  {
   for(;j<=3;++j)
    if(ok(j))
    {
     seq[i]=j;
     len++;
     i++;
     break;
    }  
   if(j>3)
   {
    j=seq[i-1]+1;
    seq[--i]=0;
    len--;
   }
   else j=1;
  }
 }
}
int main()
{
 while(cin>>n&&n>0)
 {
  len=1;

//found=false;
  seq[0]=1;
  dfs(1);
  memset(seq,0,sizeof(seq));
 }
 return 0;
}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值