首页 > 试题广场 >

DNA片段

[编程题]DNA片段
  • 热度指数:3868 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
牛牛从生物科研工作者那里获得一段字符串数据s,牛牛需要帮助科研工作者从中找出最长的DNA序列。DNA序列指的是序列中只包括'A','T','C','G'。牛牛觉得这个问题太简单了,就把问题交给你来解决。
例如: s = "ABCBOATER"中包含最长的DNA片段是"AT",所以最长的长度是2。

输入描述:
输入包括一个字符串s,字符串长度length(1 ≤ length ≤ 50),字符串中只包括大写字母('A'~'Z')。


输出描述:
输出一个整数,表示最长的DNA片段
示例1

输入

ABCBOATER

输出

2
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str=sc.nextLine();
        getDnaNum(str);
    }
 
    private static void getDnaNum(String str) {
 
        int tem=1;
        int tmp=0;
        int len=str.length();
        char[] ch= str.toCharArray();
        for(int i=0;i<len-1;i++){
            if(ch[i]=='A' || ch[i]=='C' || ch[i]=='G'|| ch[i]=='T' ){
                if(ch[i+1]=='A' ||ch[i+1]=='C' || ch[i+1]=='G'||ch[i+1]=='T' ){
                    tem++;
                }
                if(tem>tmp){
                tmp=tem;
                }
            }else{
                tem = 1;
            }
        }
        System.out.println(tmp);
    }
}
发表于 2020-01-01 19:50:32 回复(0)
我的思路是:先把字符串中不是A,T,C,G的字符换成空格,再分割空格使他们转换成相应的字符串形式,把每个字符串的长度添加到数组,然后排序输出最大的长度
public class test2 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        String str=sc.nextLine();
        str=str.replaceAll("[^ATCG]", " ");
        String[] arr=str.split(" +");

        int[] array=new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            array[i]=arr[i].length();
        }

        Arrays.sort(array);
        System.out.println(array[array.length-1]);
    }
}

发表于 2018-05-12 21:04:45 回复(0)

一种容易理解的解法,应该很容易看懂,这里就不解释了。

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        int max = 0;
        int count = 0;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == 'A' ||
                str.charAt(i) == 'T' ||
                str.charAt(i) == 'C' ||
                str.charAt(i) == 'G') {
                count++;
                max = Math.max(max, count);
            } else {
                count = 0;
            }
        }
        System.out.println(max);
    }
}
发表于 2018-03-01 18:52:06 回复(1)
import java.util.Scanner;
public class Main
{
	public static int MaxLength(String s)
	{
		int count = 0;
		int maxLength = 0;
		for (int i = 0; i < s.length(); i++)
		{
			if (s.charAt(i) == 'A' || s.charAt(i) == 'T' || s.charAt(i) == 'C' || s.charAt(i) == 'G')
				count++;
			else
				count = 0;
			if (count > maxLength)
				maxLength = count;
		}
		return maxLength;
	}

	public static void main(String[] args)
	{
		// 输入包括一个字符串s,字符串长度length(1 ≤ length ≤ 50),字符串中只包括大写字母('A'~'Z')。
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext())
		{
			String s = sc.nextLine();
			System.out.println(MaxLength(s));
		}

	}

}


发表于 2017-07-26 12:45:36 回复(0)

热门推荐

通过挑战的用户