牛牛从生物科研工作者那里获得一段字符串数据s,牛牛需要帮助科研工作者从中找出最长的DNA序列。DNA序列指的是序列中只包括'A','T','C','G'。牛牛觉得这个问题太简单了,就把问题交给你来解决。
例如: s = "ABCBOATER"中包含最长的DNA片段是"AT",所以最长的长度是2。
输入包括一个字符串s,字符串长度length(1 ≤ length ≤ 50),字符串中只包括大写字母('A'~'Z')。
输出一个整数,表示最长的DNA片段
ABCBOATER
2
一种容易理解的解法,应该很容易看懂,这里就不解释了。
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);
}
}
n=k;}
简单题。思路就是直接暴力搜索。从第一个字母开始判断,找到最长的,再从第二个字母开始。
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; } }
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); } } }
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); } }
#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;}
#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; }
#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)
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];}
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); } }
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();}}