头条笔试题目
1. 三个同样的字母连在一起,一定是拼写错误,去掉一个的就好啦:比如 helllo -> hello
2. 两对一样的字母(AABB型)连在一起,一定是拼写错误,去掉第二对的一个字母就好啦:比如 helloo -> hello
3. 上面的规则优先“从左到右”匹配,即如果是AABBCC,虽然AABB和BBCC都是错误拼写,应该优先考虑修复AABB,结果为AABCC
输入描述:
第一行包括一个数字N,表示本次用例包括多少个待校验的字符串。
后面跟随N行,每行为一个待校验的字符串。
输出描述:
N行,每行包括一个被修复后的字符串。
输入例子1:
2
helloo
wooooooow
输出例子1:
hello
woow
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int count = scanner.nextInt();
for (int i = 0; i < count; i++) {
String str = correct(scanner.next());
System.out.println(str);
}
}
public static String correct(String str) {
StringBuilder builder = new StringBuilder(str);
int index = 0, size;
while ((size = builder.length()) > index) {
if (index + 1 < size && builder.charAt(index) == builder.charAt(index + 1)) {
if (index + 2 < size && builder.charAt(index) == builder.charAt(index + 2)) {
builder.deleteCharAt(index);
} else if (index - 2 >= 0 && builder.charAt(index - 1) == builder.charAt(index - 2)) {
builder.deleteCharAt(index);
} else {
index++;
}
} else {
index++;
}
}
return builder.toString();
}
}
给定N(可选作为埋伏点的建筑物数)、D(相距最远的两名特工间的距离的最大值)以及可选建筑的坐标,计算在这次行动中,大锤的小队有多少种埋伏选择。
注意:
1. 两个特工不能埋伏在同一地点
2. 三个特工是等价的:即同样的位置组合(A, B, C) 只算一种埋伏方法,不能因“特工之间互换位置”而重复使用
解题思路:
原问题等价于,有序的正整数中去三个,最大最小差值小于等于D
假设三个张最大的为max,依次遍历max值取a[i] (i>=2)的可能性与可能情况数目。
在遍历的过程中,在a[i]的左半径D范围内的所有整数取两个(组合问题),因此关键在于求出左边界。
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner=new Scanner(System.in);
int N=scanner.nextInt();
int D=scanner.nextInt();
int[] location=new int[N];
for(int i=0;i<N;i++) {
if(scanner.hasNext()) {
location[i]=scanner.nextInt();
}
}
// 代表数字零的常量
BigInteger num=BigInteger.ZERO;
//3 3 C(3,3)=1;
//4 3 C(4,3)=4 4=1+C(3,2)
//5 3 C(5,3)=10 10=4+C(4,2)
//6 3 C(6,3)=20 20=10+C(5,2)
int j=0;
for(int i=2;i<N;i++) {
while(j<i&&(location[i]-location[j]>D)) {
j++;
}
long choose=i-j;
if(choose>=2) {
num=num.add(BigInteger.valueOf(choose*(choose-1)/2));
}
}
long mods=99997867;
System.out.println(num.mod(BigInteger.valueOf(mods)));
}
}