-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path牛客网2018校招最后一次模拟.js
86 lines (80 loc) · 2.08 KB
/
牛客网2018校招最后一次模拟.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//这次的编程题,个人感觉比较简单。我做的全是字符串类型的型。
//第一题:DNA片段
var readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal:false
});
rl.on('line', function(line){ // javascript每行数据的回调接口
var stringDNA = line;
var length = stringDNA.length;
var char;var temp = 0;
var max = 0;
for(var i =0 ;i< length ; i++){
char = stringDNA[i];
if(char == "A" || char == "T" || char == "C" || char == "G"){
temp++;
}else{
max = max>temp?max:temp;
temp = 0;
}
}
max = max>temp?max:temp;
console.log(max);
});
//第二题:最长偶串
var readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal:false
});
var x = 0, a = str[0];
x = str.indexOf(a);
// 两段字符串是否相等
if(x > str.length/2){
return 1;
}
var i = 0,indexs = [];
while(i<str.length/2 && i > -1){
indexs.push(i);
i = str.indexOf(a,i+1);
}
// console.log(indexs); //
if(indexs.length < 1){return 1;};
for(var i = indexs.length - 1 ; i>0; i--){
// console.log(str.substr(indexs[i],indexs[i]));//
// console.log(str.substr(0,indexs[i]));//
if(str.substr(indexs[i],indexs[i]) == str.substr(0,indexs[i])){
// console.log("i:",indexs[i]);//
return indexs[i]*2;
}
}
return 1;// return n;
}
//第三题:最少回文串
var readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal:false
});
rl.on('line', function(line){ // javascript每行数据的回调接口
console.log(findMax(line));
});
function findMax(str){
var sum = {};
for(var i =0;i<str.length ; i++){
sum[str[i]] = sum[str[i]] ? sum[str[i]]+1 : 1;
}
// pingjie
var duoyu = 0;
for(var key in sum){
// console.log(key,sum[key]);
if(sum[key]%2){
duoyu ++;
}
}
return duoyu;
}