1.自动校对程序
import java.util.LinkedList;
import java.util.Scanner;
public class ByteDance1 {
public static String solve(String s) {
LinkedList list = new LinkedList<>();
for (int i = 0; i < s.length(); i++) {
if (!list.isEmpty() && list.peekLast()[0] == s.charAt(i)-'a') { //当链表非空且当前元素与尾部元素相同时
if (list.peekLast()[1] == 2) //当链表尾部元素的频数等于2时,这时候不能再添加频数了
continue;
else {
//当链表尾部元素的频数等于1时,需要分类讨论,先将链表尾部元素弹出
int[] tmp = list.pollLast();
//当链表不为空且尾部频数等于2时表明出现两对相同元素的情况,所以
if (!list.isEmpty() && list.peekLast()[1] == 2)
list.add(new int[]{tmp[0],1});
else{
list.add(new int[]{tmp[0],tmp[1]+1});
}
}
} else
//链表为空或者链尾元素与当前元素不同,需要将当前元素加到尾部
list.add(new int[]{s.charAt(i)-'a',1});
}
StringBuilder sb = new StringBuilder();
while (!list.isEmpty()) {
int[] tmp = list.pollFirst();
for (int i = 0; i < tmp[1]; i++) {
sb.append((char)(tmp[0]+'a'));
}
}
return sb.toString();
}
public static void main(String[] args) {
/*
Scanner in=new Scanner(System.in);
int n=in.nextInt();
for(int i=0;i
上述解法比较复杂,但是很通用,另一种比较简洁但是难想到(改动原字符串)
//解法二
public static String solve1(String s){
int j=0;
StringBuilder sb=new StringBuilder(s);
for(int i=0;i=3 && sb.charAt(j-1)==sb.charAt(j-2) && sb.charAt(j-2)==sb.charAt(j-3))
j--;
if(j>=4 && sb.charAt(j-1)==sb.charAt(j-2) && sb.charAt(j-3)==sb.charAt(j-4))
j--;
}
return sb.substring(0,j+1);
}
2.特工
import java.util.Scanner;
public class ByteDance4 {
public static int ret;
public static int[] record;
public static void recursive(int[] arr,int idx,int start,int D){
if(idx==3){
ret%=99997867;
ret++;
return;
}
//当数组剩余待选元素的数量小于record中未确定数字的槽位数量
if(arr.length-start<3-idx)
return;
for(int i=start;iD)
break;
else
{
record[idx]=arr[i];
recursive(arr,idx+1,i+1,D);
}
}
}
public static void main(String[] args)
{
/*
Scanner in=new Scanner(System.in);
int N=in.nextInt();
int D=in.nextInt();
int[] arr=new int[N];
for(int i=0;i
上述算法是无法AC的,因为数据量大的测试数据递归太深。下面的动态规划解法:
//动态规划解法 AC
public static int solve(int[] arr, int maxDist) {
int n = arr.length;
int[] dp = new int[n];
dp[2] = arr[2] - arr[0] > maxDist ? 0 : 1;
int j = 0;
for (int i = 3; i < n; i++)
{
while (j= 2 ? 0 : num * (num - 1) / 2;
dp[i] %= 99997867;
}
int ret = 0;
for (int a : dp)
ret += a;
return ret%99997867;
}
3.特征提取
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
public class ByteDance3 {
static class feature {
int x;
int y;
public feature(int x, int y) {
this.x = x;
this.y = y;
}
public int hashCode() {
return x * 20000 + y;
}
public boolean equals(Object obj){
return this.hashCode()==obj.hashCode();
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt(); //测试样例数目
HashMap> map = new HashMap<>();
for (int i = 0; i < N; i++) {
int num = in.nextInt(); //帧数
for (int j = 0; j < num; j++) { //帧序数
int n_coords = in.nextInt();
for (int k = 0; k < n_coords; k++) {
int a = in.nextInt();
int b = in.nextInt();
feature f = new feature(a, b);
if (map.containsKey(f)) {
LinkedList tmp = map.get(f);
if (tmp.peekLast() + 1 == j) {
tmp.pollLast();
tmp.add(j);
} else {
tmp.add(j);
tmp.add(j);
}
} else {
LinkedList tmp = new LinkedList<>();
tmp.add(j);
tmp.add(j);
map.put(f, tmp);
}
}
}
int ans = 0;
for (feature f : map.keySet()) {
LinkedList tmp = map.get(f);
while (!tmp.isEmpty()) {
int start = tmp.pollFirst();
int end = tmp.pollLast();
ans = Math.max(ans, end - start + 1);
}
}
System.out.println(ans);
}
}
}
4.找零钱
import java.util.Scanner;
public class ByteDance5 {
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
int change=1024-in.nextInt();
int[] arr={64,16,4,1};
int res=0;
for(int i=0;i<4;i++)
{
res+=change/arr[i];
change%=arr[i];
}
System.out.println(res);
}
}
5.变身程序员
就是一个多源最短路径算法
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
public class ByteDance7 {
//变身程序员,多源最短路径问题 BFS
public static void main(String[] args) {
}
//确定合法延伸位置
public static boolean isValid(int i, int j, int[][] arr) {
int m = arr.length;
int n = arr.length;
return i >= 0 && i < m && j >= 0 && j < n && arr[i][j] == 1;
}
public static int solve(int[][] arr) {
int m = arr.length;
int n = arr[0].length;
int[][] dist = new int[m][n];
for (int i = 0; i < m; i++)
Arrays.fill(dist[i], -1);
Queue queue = new LinkedList<>();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (arr[i][j] == 2) {
int[] tmp = {i, j};
dist[i][j] = 0;
queue.add(tmp);
}
}
}
while (!queue.isEmpty()) {
int[] tmp = queue.poll();
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
if (Math.abs(dx + dy) == 1 && isValid(tmp[0] + dx, tmp[1] + dy, arr)) {
arr[tmp[0] + dx][tmp[1] + dy] = 2;
dist[tmp[0] + dx][tmp[0] + dy] = dist[tmp[0]][tmp[1]] + 1;
queue.add(new int[]{tmp[0] + dx, tmp[1] + dy});
}
}
}
}
int ans = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (arr[i][j] == 1 && dist[i][j] == -1)
return -1;
ans = Math.max(ans, dist[i][j]);
}
}
return ans;
}
}
6.国庆旅游(https://www.acwing.com/problem/content/865/)
这个问题有O(N^2)的暴力解法,但是无法AC,考虑到Ai+i+Aj-j的吸引力计算可以将当前Ai+i的最大值进行缓存,从而将复杂度降到O(N)
import java.util.Scanner;
class Main {
public static int solve0(int[] arr)
{
int ans=Integer.MIN_VALUE;
for(int i=0;i
7立方体塔(https://www.acwing.com/problem/content/857/)
import java.util.Scanner;
class Main {
public static int mod = 1000000007;
public static int solve(int w, int b) {
int h = 1;
while ((h + 1) * h / 2 <= w + b) //第一个大于w+b的h
h++;
h--;
System.out.print(h);
System.out.print(" ");
int[] f = new int[w + 1];
f[0]=1;
for (int i = 1; i <= h; i++) {
for (int j = w; j >= i; j--)
f[j] = (f[j] + f[j - i]) % mod;
}
int res = 0;
for (int i = 0; i <= w; i++) {
if (h * (h + 1) / 2 - i <= b)
res = (res + f[i]) % mod;
}
return res;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int w = in.nextInt();
int b = in.nextInt();
System.out.println(solve(w, b));
}
}
8.编程题
#include
using namespace std;
int main(){
int n;
while(cin>>n){
int arr[n];
for(int i=0;i=0 && arr[left]>=arr[i])
{
sum+=arr[left];
left--;
}
int right=i+1;
while(right=arr[i])
{
sum+=arr[right];
right++;
}
ans=max(ans,sum*arr[i]);
}
printf("%d\n", ans);
}
return 0;
}