首页 > 试题广场 >

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
前端:
while(line=readline()) {
      var length = 0, temp = '', reg = /[ATCG]/;
      for(let i = 0; i < line.length; i++) {
        if(reg.test(line.charAt(i)) == true) {
          temp += line.charAt(i);
          length = temp.length > length ? temp.length : length;
        } else{
          temp = '';
        }
      }
  console.log(length)
}
发表于 2017-07-27 09:17:04 回复(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 void main(String[] args) {
Scanner scanner=new Scanner(System.in);
while(scanner.hasNext()){
String string=scanner.next();
System.out.println(dna(string));
}
}
public static int dna(String string) {
int sl=string.length();
int n=0;//输出结果 计最大长度数
int k=0;//遍历结果
char a[]=string.toCharArray(); //使用String的split方法不行,需要使用char的tochararray方法
        for(int i=0;i<sl;i++){
            if('A'==a[i]||'G'==a[i]||'C'==a[i]||'T'==a[i]){
k++;
if(k>=n){//当前结果大于最大长度  赋给最大长度值n
 n=k;
}
}else{//断了!
if(k>=n){
n=k;
}
k=0;
}
}
return n;

}
}
很新手的回答 支持点个赞
编辑于 2017-10-26 09:50:49 回复(0)
本人博客地址

简单题。思路就是直接暴力搜索。从第一个字母开始判断,找到最长的,再从第二个字母开始。

代码

import java.util.*; public class Main { public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        String s = in.nextLine();

        in.close();

        System.out.println(helper(s));
    } private static int helper(String s) {

        String source = "ATGC"; int res = 0; for(int i=0;i<s.length();i++) { if(source.indexOf(s.charAt(i) + "") != -1) { int index = i; index++; while(index < s.length() && source.indexOf(s.charAt(index) + "") != -1) { index++;
                } if((index - i) > res)
                    res = index-i;
            }


        } return res;

    }
}

发表于 2017-07-27 18:23:41 回复(3)
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext()){
			String str = sc.nextLine();
			int res = 0;
			int max = Integer.MIN_VALUE;
			int length = str.length();
			String test = "ATCG";
			for(int i = 0; i < length; i++){
				res = 0;
				if(test.indexOf(str.charAt(i) + "") != -1){
					//就直到下一个不为这个为止
					if(i != 0 && res == 0) i--;
					while(i < length && test.indexOf(str.charAt(i++) + "") != -1){
						res++;
					}
					if(res > max) max = res;
				}
			}
			System.out.println(max);
		}
	}
}

发表于 2017-07-25 21:43:31 回复(2)
我的思路是:先把字符串中不是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 s = sc.nextLine();
        String[] str=new String[s.length()];
        //记录最大数
        int maxNum = 0;
        //记录每次匹配得数
        int num = 0;
        //DNA片段
        String dna = "ATCG";
        //将字符串遍历放到字符串数组中
        for (int i = 0; i < s.length(); i++) {
            str[i] = String.valueOf(s.charAt(i));
        }
        
        for (int j = 0; j < s.length(); j++) {
            int indexOf = dna.indexOf(str[j]);
            if(indexOf != -1){
                num++;
                if (num > maxNum) {
                    maxNum = num;
                }
            }else {
                num = 0;
            }
        }
        System.out.println(maxNum);
        
    }
}

发表于 2018-04-18 17:02:37 回复(0)

换个思路就好,把不是ATCG的字母的干扰去掉,全换成小写x,再分数组,看元素长度哪个最长

var s = readline()
    var a = s.replace(/[^ATCG]+/g, 'x');
    var arr = a.split('x').map(function(ele, ind) {
        return ele.length
    })
    console.log(Math.max.apply(null, arr))
编辑于 2017-10-16 20:56:42 回复(0)
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String s = in.nextLine();
System.out.print(new Main().getMaxLength(s));
}
public int getMaxLength(String s){
int current = 0, max=0;
for(int i = 0; i < s.length(); ++i){
if (isATCG(s.charAt(i))) {
current++;
} else {
max = Math.max(current, max);
current=0;
}
max = Math.max(current, max);
}
return max;
}
private boolean isATCG(char c){
if((c=='A') || (c=='T') || (c=='C') || (c=='G')) return true;
return false;
}
}
编辑于 2017-10-01 13:02:56 回复(0)
#include <iostream>
#include <string>
using namespace std;
intmain()
{
    string str;
    getline(cin,str,'\n');
    intk0 = 0;
    intk = 0;
    for(inti=0;i<str.length();i++)
    {
        if(str[i]=='A'||str[i]=='T'||str[i]=='C'||str[i]=='G')
            k++;       
        else
        {
            if(k>k0)
            {
                k0 = k;
            }
            k=0;
        }
    }
    if(k>k0)
    {
        k0 = k;
    }
    cout<<k0<<endl;
    return0;
}

发表于 2017-08-01 11:18:03 回复(0)
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string str;
	cin >> str;
	int minlen = -1;
	for (int i = 0; i < str.size(); )
	{
		int count = 0;
		int j = i;
		while ((str[j] == 'A' || str[j] == 'C' || str[j] == 'G' || str[j] == 'T') && (j < str.size()))
		{
			++count;
			++j;
		}
		i = ++j;
		if (minlen < count)
			minlen = count;
	}
	cout << minlen << endl;
	return 0;
}

发表于 2017-07-26 08:41:55 回复(0)
#include<stdio.h>
#include<string>
#include<iostream>
using namespace std;
int main(){
	string x;
	while(cin>>x){
		int i,j,Max=0;
		for(i=0;i<x.length();i++){
			string tmp="";
			for(j=i;j<x.length();j++)
				if(x[j]=='A'||x[j]=='G'||x[j]=='C'||x[j]=='T'){
					tmp+=x[j];
					if(Max<tmp.length()) Max=tmp.length();
				}else
					break;
		}
		printf("%d\n",Max);
	}
}//数据太小了  直接遍历所有的字串就可以了 复杂度O(N^2)

发表于 2017-08-02 19:07:24 回复(4)
s = input()

a = 'ACGT'
b = 0
for i in range(len(s)):
    for j in range(i+1,len(s)+1):                  
        for x in s[i:j]:
            if x not in a:
                break
        else:
            if len(s[i:j]) > b:
                b = len(s[i:j])
print(b)
发表于 2022-06-02 11:41:23 回复(0)
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)
fscanf(STDIN, "%s", $s);
    getMaxLength($s);
 
    functiongetMaxLength($s){
        //$s = 'ABCBOACGTTATERABCBOACGTTATERABCBOACGTTATER';
        $pre= "/[ATCG]+/";
        preg_match_all($pre, $s, $match);
         
        $tempData= array();
        foreach($match[0] as$key=>$val){
            $tempData[$key] = strlen($val);
        }
        rsort($tempData);
        echo$tempData[0];
    }

发表于 2018-08-27 11:29:04 回复(0)
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//使用的是正则表达式
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str= sc.next();

        String pattern = "[ATCG]+";
        // 创建 Pattern 对象
        Pattern r = Pattern.compile(pattern);
        // 现在创建 matcher 对象
        Matcher m = r.matcher(str);
        int mlen=0;
        while(m.find()) {
            if(m.group().length()>mlen)
                mlen=m.group().length();
        }
        
        System.out.println(mlen);
    }

}
发表于 2018-04-01 16:59:16 回复(0)
importjava.util.Scanner;
public class Main{
public static int maxLong(String str){
StringBuilder DNA = new StringBuilder("ATCG");
int count = 0;
int temp = 0;
int length = str.length();
for(int i = 0;i<length;i++){
if(DNA.indexOf(str.charAt(i)+"")!=-1){
count++;
temp = temp>=count?temp:count;
}
else{
temp = temp>=count?temp:count;
count = 0;
}
}
returntemp;
}
public static void main(String[] args){
Scanner scanner = newScanner(System.in);
String str = scanner.nextLine();
System.out.println(maxLong(str));
}
}
编辑于 2018-03-16 10:02:45 回复(0)
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * 利用正则,进行匹配指定的串,然后统计匹配项中的最大长度
 *
 */
public class NiuNiu {
    public static void main(String[] arags) {
        Scanner s = new Scanner(System.in);
        String ss = s.nextLine();
        Pattern p = Pattern.compile("([ATCG])+");
        Matcher matcher = p.matcher(ss);
        int len = 0;
        while (matcher.find()) {
            String sss = matcher.group();
            int l = sss.length();
            if(l>len)
                len = l;
        }
        System.out.println(len);
    }
}


发表于 2018-03-14 14:22:34 回复(0)
#include<iostream>
#include<string>
using namespace std;
int main()
{
    string str;
    getline(cin,str,'\n');
    int count=0,max=0;
    for(int i=0;i<str.length();++i)
    {
        if(str[i]!='A'&&str[i]!='T'&&str[i]!='C'&&str[i]!='G')
        {count=0;
            continue;}
        else
            ++count;
        if(count>max)
            max=count;
        
    }
    cout<<max<<endl;
    return 0;
}
发表于 2018-03-13 16:08:08 回复(0)
importjava.util.Scanner;
 
publicclassMain {
 
     
    publicstaticvoidmain(String[] args) {
         
        Scanner in = newScanner(System.in);
        while(in.hasNext()) {
            String s = in.next();
            intcnt = 0;
            intmax = 0;
            for(inti = 0;i < s.length();i ++) {
                if(s.charAt(i) == 'A'|| s.charAt(i) == 'T'|| s.charAt(i) == 'C'|| s.charAt(i) == 'G') {
                    cnt ++;
                    if(cnt > max) {
                        max = cnt;
                    }
                } else{
                    cnt = 0;
                }
            }
            System.out.println(max);
        }
        in.close();
    }
}

发表于 2018-01-17 15:02:39 回复(0)