洛谷—— P2562 [AHOI2002]Kitty猫基因编码

 P2562 [AHOI2002]Kitty猫基因编码

题目描述

小可可选修了基础生物基因学。教授告诉大家 Super Samuel 星球上 Kitty猫的基因的长度都是 2 的正整数次幂 ), 全是由两种不同的基因单元组成的。 这两种不同的基因单元分别记成 0 和 1,于是 Kitty 猫基因可以写成一个 01 串表达式 。

为了便于分析和降低数据存储量,教授发明了 ABC 编码规则。该编码规则是不断地按照

对 Kitty 猫基因 01 串表达式 进行改写, 直至最终被改写成只含有字符 “ A”、“ B”、“ C”的符号串。

请你编写程序帮助小可可求出 Kitty 猫基因的 ABC 编码以协助教授开展科研工作。

输入输出格式

输入格式:

 

文件中以一行的形式存放了一个 Kitty 猫基因的 01 串表达式。

 

输出格式:

 

以一行的形式输出这个 Kitty 猫基因的 ABC 编码。

 

输入输出样例

输入样例#1:  复制
00
输出样例#1:  复制
A
输入样例#2:  复制
01001011
输出样例#2:  复制
CCCABACCBAB

 

dfs

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 100100
using namespace std;
int l,sum;
char ch[N],ans[N],w,q;
int read()
{
    int x=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}
void dfs(int a,int b,int e)
{
    if(a>b) return ;
    w=q=ch[a];
    for(int i=a+1;i<=b;i++)
     if(ch[i]!=w) {
      q=ch[i];break;    
     }
    if(q==w)
    {
        if(w=='0') ans[++sum]='A';
        else ans[++sum]='B';
        if(b<=l/2&&e<=l&&b+1<=e) dfs(b+1,e,e<<1);
        if(b>l/2&&e<=l) dfs(b+1,e,e*2-l/2);
    }
    else 
    {
        ans[++sum]='C';
        if(b>=a) dfs(a,(a+b)>>1,b);
    }
}
int main()
{
    cin>>ch+1;l=strlen(ch+1);
    dfs(1,l,l);
    for(int i=1;i<=sum;i++)
     printf("%c",ans[i]);
    return 0;
}
40分代码、、

 

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 100100
using namespace std;
int l,sum;
char ch[N],ans[N],w,q;
int read()
{
    int x=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}
void dfs(int a,int b,int e)
{
    if(a>b) return ;
    w=q=ch[a];
    for(int i=a+1;i<=b;i++)
     if(ch[i]!=w) {
      q=ch[i];break;    
     }
    if(q==w)
    {
        if(w=='0') ans[++sum]='A';
        else ans[++sum]='B';
    }
    else 
    {
        ans[++sum]='C';
        int mid=a+b>>1;
        dfs(a,mid,mid-a+1);
        dfs(mid+1,b,b-mid);
    }
}
int main()
{
    cin>>ch+1;l=strlen(ch+1);
    dfs(1,l,l);
    for(int i=1;i<=sum;i++)
     printf("%c",ans[i]);
    return 0;
}

 

转载于:https://www.cnblogs.com/z360/p/7966186.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值