1.万万没想到之聪明的编辑
我叫王大锤,是一家出版社的编辑。我负责校对投稿来的英文稿件,这份工作非常烦人,因为每天都要去修正无数的拼写错误。但是,优秀的人总能在平凡的工作中发现真理。我发现一个发现拼写错误的捷径:
1. 三个同样的字母连在一起,一定是拼写错误,去掉一个的就好啦:比如 helllo -> hello
2. 两对一样的字母(AABB型)连在一起,一定是拼写错误,去掉第二对的一个字母就好啦:比如 helloo -> hello
3. 上面的规则优先“从左到右”匹配,即如果是AABBCC,虽然AABB和BBCC都是错误拼写,应该优先考虑修复AABB,结果为AABCC
我特喵是个天才!我在蓝翔学过挖掘机和程序设计,按照这个原理写了一个自动校对器,工作效率从此起飞。用不了多久,我就会出任CEO,当上董事长,迎娶白富美,走上人生巅峰,想想都有点小激动呢!
……
万万没想到,我被开除了,临走时老板对我说: “做人做事要兢兢业业、勤勤恳恳、本本分分,人要是行,干一行行一行。一行行行行行;要是不行,干一行不行一行,一行不行行行不行。” 我现在整个人红红火火恍恍惚惚的……
请听题:请实现大锤的自动校对程序
数据范围:1≤n≤50 ,每个用例的字符串长度满足 1≤L≤1000
import java.util.Scanner;
// 暴力求解
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a = in.nextInt();
while (a-- > 0) {
String s = in.next();
for (int i = 2; i < s.length(); i++) {
if (s.charAt(i) == s.charAt(i - 1) && s.charAt(i - 1) == s.charAt(i - 2)) {
s=removeChar(s, i);
// System.out.println(s);
i--;
}
if (s.length() < 3)
break;
}
for (int i = 3; i < s.length(); i++) {
if (s.charAt(i) == s.charAt(i - 1) && s.charAt(i - 2) == s.charAt(i - 3)) {
s=removeChar(s, i);
// System.out.println(s);
i--;
}
if (s.length() < 4)
break;
}
System.out.println(s);
}
}
public static String removeChar(String s, int index) {
return s.substring(0, index) + s.substring(index + 1, s.length());
}
}
2.万万没想到之抓捕孔连顺
我叫王大锤,是一名特工。我刚刚接到任务:在字节跳动大街进行埋伏,抓捕恐怖分子孔连顺。和我一起行动的还有另外两名特工,我提议
1. 我们在字节跳动大街的 N 个建筑中选定 3 个埋伏地点。
2. 为了相互照应,我们决定相距最远的两名特工间的距离不超过 D 。
我特喵是个天才! 经过精密的计算,我们从X种可行的埋伏方案中选择了一种。这个方案万无一失,颤抖吧,孔连顺!
……
万万没想到,计划还是失败了,孔连顺化妆成小龙女,混在cosplay的队伍中逃出了字节跳动大街。只怪他的伪装太成功了,就是杨过本人来了也发现不了的!
请听题:给定 N(可选作为埋伏点的建筑物数)、 D(相距最远的两名特工间的距离的最大值)以及可选建筑的坐标,计算在这次行动中,大锤的小队有多少种埋伏选择。
注意:
1. 两个特工不能埋伏在同一地点
2. 三个特工是等价的:即同样的位置组合( A , B , C ) 只算一种埋伏方法,不能因“特工之间互换位置”而重复使用
数据范围: 0<n,d≤106
import java.util.Scanner;
// i,j作为左右边界,根据pos[j]-pos[i] 与 d的大小关系,变化i,j
public class Main {
public static void main(String[] args) throws IOException{
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();int d=sc.nextInt();
int pos[]=new int[n];
for(int i=0;i<n;i++){
pos[i]=sc.nextInt();
}
Long ans=0L;
int i=0;int j=2;
while(j<n){
if(pos[j]-pos[i]<=d){
//转为long类型
long tmp=j-i-1;
ans+=((tmp)*(tmp+1)/2);
j++;
}else{
if(j-i==2){
i++;j++;
}else{
while(j-i>2&&pos[j]-pos[i]>d){
i++;
}
}
}
}
System.out.println(ans%99997867);
}
}
3.雀魂启动!
小包最近迷上了一款叫做雀魂的麻将游戏,但是这个游戏规则太复杂,小包玩了几个月了还是输多赢少。
于是生气的小包根据游戏简化了一下规则发明了一种新的麻将,只留下一种花色,并且去除了一些特殊和牌方式(例如七对子等),具体的规则如下:
总共有36张牌,每张牌是1~9。每个数字4张牌。
你手里有其中的14张牌,如果这14张牌满足如下条件,即算作和牌
14张牌中有2张相同数字的牌,称为雀头。
除去上述2张牌,剩下12张牌可以组成4个顺子或刻子。顺子的意思是递增的连续3个数字牌(例如234,567等),刻子的意思是相同数字的3个数字牌(例如111,777)
例如:
1 1 1 2 2 2 6 6 6 7 7 7 9 9 可以组成1,2,6,7的4个刻子和9的雀头,可以和牌
1 1 1 1 2 2 3 3 5 6 7 7 8 9 用1做雀头,组123,123,567,789的四个顺子,可以和牌
1 1 1 2 2 2 3 3 3 5 6 7 7 9 无论用1 2 3 7哪个做雀头,都无法组成和牌的条件。
现在,小包从36张牌中抽取了13张牌,他想知道在剩下的23张牌中,再取一张牌,取到哪几种数字牌可以和牌。
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
// dfs 搜索所有可能
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int []array=new int[14];
List<Integer> ans=new ArrayList<>();
for(int i=0;i<13;i++){
array[in.nextInt()]++;
}
for(int i=1;i<10;i++){
if(array[i]==4){
continue;
}
array[i]++;
if(ishu(array,14,1)){
ans.add(i);
}
array[i]--;
}
if(ans.size()==0){
System.out.print(0);
}else{
System.out.print(ans.get(0));
for(int i=1;i<ans.size();i++){
System.out.printf(" "+ans.get(i));
}
}
System.out.println();
}
public static boolean ishu(int []array,int cur_num,int state){
if(cur_num==0){
return true;
}
// 找对子
if(state==1){
for(int i=1;i<10;i++){
if(array[i]>=2){
array[i]-=2;
cur_num-=2;
state=2;
if(ishu(array,cur_num,state)){
state=1;
array[i]+=2;
cur_num+=2;
return true;
}
state=1;
array[i]+=2;
cur_num+=2;
}
}
}else{
//找刻字或者顺子
for(int i=1;i<10;i++){
if(array[i]>=3){
array[i]-=3;
cur_num-=3;
if(ishu(array,cur_num,state)){
array[i]+=3;
cur_num+=3;
return true;
}
array[i]+=3;
cur_num+=3;
}
if(array[i]>=1&&array[i+1]>=1&& array[i+2]>=1){
array[i]-=1;array[i+1]-=1;array[i+2]-=1;
cur_num-=3;
if(ishu(array,cur_num,state)){
cur_num+=3;
array[i]+=1;array[i+1]+=1;array[i+2]+=1;
return true;
}
cur_num+=3;
array[i]+=1;array[i+1]+=1;array[i+2]+=1;
}
}
}
return false;
}
}
4.特征提取
小明是一名算法工程师,同时也是一名铲屎官。某天,他突发奇想,想从猫咪的视频里挖掘一些猫咪的运动信息。为了提取运动信息,他需要从视频的每一帧提取“猫咪特征”。一个猫咪特征是一个两维的vector<x, y>。如果x_1=x_2 and y_1=y_2,那么这俩是同一个特征。
因此,如果喵咪特征连续一致,可以认为喵咪在运动。也就是说,如果特征<a, b>在持续帧里出现,那么它将构成特征运动。比如,特征<a, b>在第2/3/4/7/8帧出现,那么该特征将形成两个特征运动2-3-4 和7-8。
现在,给定每一帧的特征,特征的数量可能不一样。小明期望能找到最长的特征运动。
import java.util.*;
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
static class Node {
int x, y;
public Node() {}
public Node(int x, int y) {
this.x = x;
this.y = y;
}
//重写equal和hashcode 方法,Node作为map的key,使得x,y值分别相等的两个Node判断为对象相等。
public boolean equals(Object obj) {
if (this ==obj) {
return true;
}
if (obj == null ||obj.getClass() != this.getClass()) {
return false;
}
Node tmp_node = (Node) obj;
return tmp_node.x==x && tmp_node.y==y;
}
public int hashCode() {
return Objects.hash(x, y);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int ans = 0;
Map<Node, Integer> pre_map = new HashMap<>();
for (int i = 0; i < m; i++) {
Map<Node, Integer> cur_map = new HashMap<>();
cur_map.clear();
int feature_num = sc.nextInt();
for (int j = 0; j < feature_num; j++) {
Node tmp_node = new Node(sc.nextInt(), sc.nextInt());
int count = pre_map.getOrDefault(tmp_node, 0) + 1;
cur_map.put(tmp_node, count);
ans = Math.max(count, ans);
}
pre_map.clear();
pre_map = cur_map;
}
System.out.println(ans);
}
}
5.毕业旅行问题
小明目前在做一份毕业旅行的规划。打算从北京出发,分别去若干个城市,然后再回到北京,每个城市之间均乘坐高铁,且每个城市只去一次。由于经费有限,希望能够通过合理的路线安排尽可能的省一些路上的花销。给定一组城市和每对城市之间的火车票的价钱,找到每个城市只访问一次并返回起点的最小车费花销。
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc .nextInt();
int [][]map = new int [n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
map[i][j] = sc.nextInt();
}
}
/*
dp[][] : 城市 k 到 集合{1,2,...,n},再回到出发点 0 最短路径
状态转移方程:dp[0]{1,2,3} = min{ map[0][1]+dp[1]{2,3} , map[0][2]+ dp[2]{1,3} , map[0][3]{1,2}}
初始状态: dp[k]{}=dp[k][0] 表示从城市k到出发点0 的路径,dp[k][0]=map[k][0]
根据初始状态,填充dp[][]集合,从小集合推到大集合
dp[k][t]=map[k][t]+dp[t][0]
*/
int dp[][] = new int[n][1 << (n - 1)];
for (int i = 0; i < n; i++) {
dp[i][0] = map[i][0];
}
/*
遍历两次,第一次遍历集合{1},{2},{3},{1,2},{1,3},{2,3},{1,2,3},...
集合的表示:
状态压缩的方法,用 int 数字的每一位,表示dp[n]{p1,p2,...,pm}中的{p1,p2,...,pm},即Pm存在等价于int 第m位为1
第二次,遍历出发的城市
*/
for (int p = 1; p < 1 << (n - 1); p++) {
for (int c = 0; c < n; c++) {
dp[c][p] = Integer.MAX_VALUE >> 1;
if (in_Set(c, p)) {
//如果起点c在集合p中,跳过
continue;
}
// 此时计算 dp[c]{p1,p2,p3,...}, 依次枚举子问题
for (int k = 1; k < n; k++) {
if (in_Set(k, p)) {
int op = unmark(p, k);
dp[c][p] = Math.min(map[c][k] + dp[k][op], dp[c][p]);
}
}
}
}
System.out.println(dp[0][(1 << (n - 1)) - 1]);
}
private static boolean in_Set(int i, int p) {
//判断int 二进制值 p 的第i位是否为1,城市0 返回的是false
return (p & (1 << (i - 1))) != 0;
}
private static int unmark(int p, int k) {
return (p & (~(1 << (k - 1))));
}
}
6.找零
Z国的货币系统包含面值1元、4元、16元、64元共计4种硬币,以及面值1024元的纸币。现在小Y使用1024元的纸币购买了一件价值为N(0<N≤1024)的商品,请问最少他会收到多少硬币?
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=1024-sc.nextInt();
int ans=0;
int array[]=new int []{64,16,4,1};
for(int i=0;i<4;i++){
ans+=n/array[i];
n%=array[i];
}
System.out.println(ans);
}
}
7.机器人跳跃问题
机器人正在玩一个古老的基于DOS的游戏。游戏中有N+1座建筑——从0到N编号,从左到右排列。编号为0的建筑高度为0个单位,编号为i的建筑的高度为H(i)个单位。
起初, 机器人在编号为0的建筑处。每一步,它跳到下一个(右边)建筑。假设机器人在第k个建筑,且它现在的能量值是E, 下一步它将跳到第个k+1建筑。它将会得到或者失去正比于与H(k+1)与E之差的能量。如果 H(k+1) > E 那么机器人就失去 H(k+1) - E 的能量值,否则它将得到 E - H(k+1) 的能量值。
游戏目标是到达第个N建筑,在这个过程中,能量值不能为负数个单位。现在的问题是机器人以多少能量值开始游戏,才可以保证成功完成游戏?
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
static boolean check(int []array,int e,int max_h){
//检查初始能量为mid的时候,
for(int j=0;j<array.length;j++){
e=2*e-array[j];
if(e>=max_h){
return true;
}else if(e<0){
return false;
}
}
return true;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int []array = new int[n];
int max_h = 0;
for (int i = 0; i < n; i++) {
array[i] = sc.nextInt();
max_h = Math.max(array[i], max_h);
}
int e_left = 0; int e_right = max_h;
while (e_left<e_right){
int mid=(e_left+e_right)/2;
if(check(array,mid,max_h)){
e_right=mid;
}else{
e_left=mid+1;
}
}
System.out.println(e_left);
}
}