3-7 uva 1368 - DNA Consensus String

背景:最早题目读错以为是在那几个中选一个,结果是自己造一个····改完wa一次,我感觉不会是最后一个空行的问题吧,改了还是wa。觉得不科学,发现样例中都是下一组的长度比上一组长,感觉自己的有点问题,试了一下果然下一组比上一组短的话就会出错。果然加了memset之后a了。

学习:本题只要找出每一列hamming值的和最小的字母这样就能保证找出来的整个序列最小,并且利用一个数组ACGT按字典序,这样最先找到的肯定是字典序最小的就不用管了。犯的错误就在于如果利用数组存储将要输出的字符时 , 需要每组都进行初始化,避免之前的字符还存在那个数组中跟着输出。或者换一个想法将每个找到的字符直接打印出来不存储。 还有一个就是在输入的时候如果保证没有空字符就可以直接用scanf("%s"), 不需要利用getchar变得更加麻烦。

#include<stdio.h>
#include<string.h>
char s[52][1005];
char b[5]="ACGT";
char a[1005];   //储存目前hamming最小且字典序最小的符合条件的序列 
int f(int m,int n)    //m代表第几列   n代表总共有几行 
{
<span style="white-space:pre">	</span>int i,j,count=0;
<span style="white-space:pre">	</span>int min=10006;
<span style="white-space:pre">	</span>for(i=0;i<4;i++)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>count=0;
<span style="white-space:pre">		</span>for(j=0;j<n;j++)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>if(b[i]!=s[j][m])
<span style="white-space:pre">			</span>count++;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>if(min>count)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>min=count;
<span style="white-space:pre">			</span>a[m]=b[i];
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>//printf("%c",a[m]);  //也可以采用这种写法 就不需要memset 
<span style="white-space:pre">	</span>return min;
}
int main ()
{
<span style="white-space:pre">	</span>int t;
<span style="white-space:pre">	</span>scanf("%d",&t);
<span style="white-space:pre">	</span>while (t--)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>int x,y;
<span style="white-space:pre">		</span>scanf("%d %d",&x,&y);
<span style="white-space:pre">		</span>int i,j;
<span style="white-space:pre">		</span>int sum=0;
<span style="white-space:pre">		</span>memset(a,'\0',sizeof(a));
<span style="white-space:pre">		</span>for(i=0;i<x;i++)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>/*for(j=0;j<y;j++)
<span style="white-space:pre">			</span>{
<span style="white-space:pre">				</span>s[i][j]=getchar();
<span style="white-space:pre">				</span>if(s[i][j]=='\n'||s[i][j]==' ')
<span style="white-space:pre">				</span>j--;
<span style="white-space:pre">			</span>} */
<span style="white-space:pre">			</span>scanf("%s",s[i]);
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>for(j=0;j<y;j++)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>sum+=f(j,x);     //j代表第几列 
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>printf("%s\n",a);
<span style="white-space:pre">		</span>printf("%d\n",sum);
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值