蓝桥杯直播课(软件开发类)记录2021-3-13

Java语言开发环境:

  • JDK 1.8
  • Eclipse-java-2020-6
  • API帮助文档

大赛考察范围

  • 侧重考察对算法和数据结构的灵活运用的能力
  • 计算机算法:枚举、排序、搜索、计数、贪心、动态规划、图论、数论、博弈论、概率论、计算几何、字符串算法等
  • 数据结构:数组、对象/结构、字符串、队列、栈、堆、树、图、平衡树/线段树、复杂数据结构、嵌套数据结构等

准备建议

  • 最近四年的真题
  • 对算法及数据结构能解决什么问题有大致了解(OI-WIKIhttps://github.com/metaphysis/Code)
  • 注重基础(枚举,搜索,常见动态规划类型,哈希表,滑动窗口)
  • 多写程序(蓝桥云课)
  • 蓝桥杯练习系统

直播课真题解析

人物相关性分析


【输入格式】
第一行包含一个整数 K。 第二行包含一行字符串,只包含大小写字母、标点符号和空格。长度不超 过 1000000。
【输出格式】
输出一个整数,表示 Alice 和 Bob 同时出现的次数。
【样例输入】
20
This is a story about Alice and Bob.Alice wants to send aprivate message to Bob.
【样例输出】
2
【评测用例规模与约定】
对于所有评测用例,1≤ K ≤1000000。
时间限制:1.0s
内存限制:512.0MB

解析过程

在这里插入图片描述
注意:上面第二个Alice的索引应为36,空格和标点符号算一个字符
遍历整个字符串记录Alice和Bob的索引位置
在这里插入图片描述
A1至An,B1至Bn等均为Alice和Bob的第一个字符的索引位置。如A1下图画图所示:
在这里插入图片描述
只要第i个Bob的索Bi在这个[A1-K-3,A1+5+K]的这个范围内,说明此Bob与Alice同时出现。
在这里插入图片描述

代码设计过程

在这里插入图片描述
alice和bob数组里面存储的是他们在字符串索引中出现的位置索引,对每一个Alice遍历,寻找与其同时出现的Bob的次数,确定Bob的左右索引位置及bob[lp]和bob[rp],从而得到对此Alice符合同时出现条件的Bob的个数为rp-lp+1.以上为C++代码实现过程。
以下为Java代码详细设计过程:

package com.mike.alice;

import java.util.Scanner;
import java.util.Vector;

/**
 * 
 * @Description
 * @author mike
 * @version
 * @date 2021-3-14下午3:30:22
 *
 */
public class AliceAndBob {

	//若为26个字母返回false,否则返回true
	static boolean check(char c){
		if ((c>='a'&&c<='z')||(c>='A'&&c<='Z')) {
			return false;
		}
		return true;
	}
	
	public static void main(String[] args) {
		Vector<Integer> alice=new Vector<>();
		Vector<Integer> bob=new Vector<>();
		
		//此处写两个scanner是为了实现两行的输入格式,如果只写一个
		//scanner会导致写第一行数字20,按enter健程序就运行啦。
		//根本来不及写字符串。
		Scanner scanner=new Scanner(System.in);
		Scanner scanner2=new Scanner(System.in);
		int K=scanner.nextInt();
		String s=scanner2.nextLine();//复制多行文本只取第一行文本
//		System.out.println(s);//调试程序用
		
		//统计Alice和Bob出现的索引位置
		//对每一个i看它的第i-1和第i+5位置是什么符号,以确保Alice单独出现
		//i==0和i+5==s.length()意思是截取最前面5个字符和最后面5个字符
		//charAt(i+5)有可能索引越界,但因为i+5==s.length()为真时会逻辑短路,不会执行check(s.charAt(i+5))
		//以引发越界异常
		//charAt(i-1)有可能索引为负,但因为i==0为真时会逻辑短路,不会执行check(s.charAt(i-1))
		//以引发索引为负异常
		for (int i = 0; i+5 <= s.length(); i++) {
			if ((i==0||check(s.charAt(i-1)))&&(i+5==s.length()||check(s.charAt(i+5)))) {
				if (s.substring(i,i+5).equals("Alice")) {
					alice.add(i);				
				}
			}
		}
		
//		System.out.println("alice="+alice);//调试无误
		
		bob.add(-10000000);//方便处理边界问题。
		
		for(int i=0;i+3<=s.length();i++){
			if ((i==0||check(s.charAt(i-1)))&&(i+3==s.length()||check(s.charAt(i+3)))) {
				if (s.substring(i, i+3).equals("Bob")) {
					bob.add(i);
				}
			}
		}
		bob.add(10000000);//方便处理边界问题,K和n取值范围<=1000000,这里夺取一个量级,好处理边界问题
//		System.out.println("bob="+bob);
		int ans=0;//记录Alice和Bob同时出现的次数
		int ca=alice.size();
		int cb=bob.size();
//		System.out.println("alice.size="+ca);
//		System.out.println("bob.size="+cb);
		
		int lp=0,rp=0;
		for(int i=0;i<ca;i++){
			while (bob.get(lp)<alice.get(i)-3-K) {
				lp++;
			}
			//循环结束得最左边界lp(包括lp)
			while(bob.get(rp+1)<=alice.get(i)+K+5){
				rp++;
			}
			//循环结束得最右边界rp(包括rp)
			if (rp-lp+1>0) {
				ans+=rp-lp+1;
			}
		}
		System.out.println(ans);
	}
}

测试用例如下:

第一个用例

20
This is a story about Alice and Bob.Alice wants to send aprivate message to Bob.
alice=[22, 36]
bob=[-10000000, 32, 76, 10000000]
alice.size=2
bob.size=4
2

第二个用例

1000000
This is a story about Alice and Bob.Alice wants to send aprivate message to Bob.
alice=[22, 36]
bob=[-10000000, 32, 76, 10000000]
alice.size=2
bob.size=4
4

第三个用例完全按照题目的输入输出格式要求即答案只有一个才算得分。严格按照格式得本题最终结果(eclipse中Console控制台输出结果)

20
This is a story about Alice and Bob.Alice wants to send aprivate message to Bob.
2

第四个用例

100000
This is a story about Alice and Bob.Alice wants to send aprivate message to Bob.This is a story about Alice and Bob.Alice wants to send aprivate message to Bob.
16

感受:完完全全做对这道题真是麻烦。编码一步一个坑。改了好久,总是这个问题哪个问题,我都快崩溃啦,出现这个错误那个错误,欢迎同学有更好的测试用例测试我的程序的通用性和健壮性,或指出我程序或解题思路的错误,请不吝赐教
在这里插入图片描述
【评测用例规模与约定】
对于 20% 的评测用例,1 ≤ n ≤ 10;对于 40% 的评测用例,1 ≤ n ≤ 100;对于 50% 的评测用例,1 ≤ n ≤ 1000;对于 60% 的评测用例,1 ≤ n ≤ 10000;对于所有评测用例,1 ≤ n ≤ 100000。

解析

如果使用暴力算法,确定左边确定右边就确定一个字串,则需要两重循环遍历所有字串得出结果(如上图按行计算),其时间复杂度为
O ( 1 0 5 ∗ 1 0 5 ) > > O ( 1 0 8 ) O(10^5*10^5)>>O(10^8) O(105105)>>O(108)
计算机一秒运算的次数达10的8次方,一秒内即可得结果,如果算法时间复杂度为O(10^10),大了两个量级,一秒内算不过来。所以时间复杂度O(n ^2)的枚举算法显然不高效。
如下是新的思路
在这里插入图片描述
如上图从列的方向来看,即对子串ABABC中的第一个A那一列,包含此A的所有子串中此A恰好出现一次的子串个数,即为那一列红A的个数。遍历第二列即其他列,思路一样,把所有列的符和条件的子串数目相加即为本题所求结果。即上图中所有红色字母的个数。此处结合S的分值f(S)的定义来理解。
在这里插入图片描述
符合上图的条件的字串s[l…r]中,红a恰好出现一次。
在这里插入图片描述

C语言核心代码如下
在这里插入图片描述
整个程序设计的关键是:对于输入字符串中的每一个字符需要确定恰好包含此字符一次的子串其左右端点的取值范围。例如对于红a字符需要确定它的前驱绿a和后驱蓝a才能确定子串左右端点取值范围,由此左端点几种取法,右端点几种取法。两者相乘即可得到符合条件的子串个数。

详细Java代码设计如下:

package com.mike.alice;

import java.util.Scanner;

/**
 * 
 * @Description
 * @author mike
 * @version
 * @date 2021-3-14下午3:29:57
 *
 */
public class Main {

	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		String s=scanner.nextLine();
		int n=s.length();//记录输入字符串长度
		int[] last=new int[26];//里面存放26个字母的临时索引值,以索引0表示a,索引1表示b
		int[] pre=new int[n];//pre[i]表示索引为i的字符的前驱字符所在位置
		int[] nxt=new int[n];//nxt[i]表示索引为i的字符的后继字符所在位置
		for(int i=0;i<26;i++){
			last[i]=-1;//此数组存储的是字符a等的索引值。作为一个临时变量
		}
		//存储字符串中每个字符的前驱
		for(int i=0;i<n;i++){
			int x=s.charAt(i)-'a';
			pre[i]=last[x];//i=0处为a,则x=0,a的前驱索引为-1,假如i=3时为第2个a,此时该a的前驱last[0]=0
			last[x]=i;
		}
		for(int i=0;i<26;i++){
			last[i]=n;//此处初始化取n和上面取-1可方便处理边界问题以及前驱后继寻找问题
		}
		for(int i=n-1;i>=0;i--){
			int x=s.charAt(i)-'a';
			nxt[i]=last[x];
			last[x]=i;
		}
		int ans=0;//记录符合条件的子串的个数,这个值即为本题最终结果
		for(int i=0;i<n;i++){
			ans+=(i-pre[i])*(nxt[i]-i);//此处当pre[i]=-1,说明i无前驱为i+1,当nxt[n]=n,说明i无后继,则为n-i
		}
		System.out.println(ans);
	}
}

测试用例如下

abcdkdabdjsfksfabdskfsjfksbasdkfjskdfjsaiefsjdfskfjasldfjaksdfjsalfkajfddjfskskdfjsdfiw
9055
ababc
21
aaa
3
注解:此次切不要将其与f("aaa")=0相混淆。作者就混淆了一次。

感受:此题用传统暴力算法解决会很直观但会超时,需要对算法进行优化,即从列开始计数就简单多啦。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
上图改正一下是:首先s[l…r]必须包含s[i],所以l<=i<=r
在这里插入图片描述
在这里插入图片描述

Java代码详细设计过程

package com.mike.alice;

import java.util.Scanner;

/**
 * 
 * @Description
 * @author mike
 * @version
 * @date 2021-3-14下午4:13:06
 *
 */
public class Main2 {

	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		String s=scanner.nextLine();
		int n=s.length();//记录输入字符串长度
		int[] last=new int[26];//里面存放26个字母的临时索引值,以索引0表示a,索引1表示b
		int[] pre=new int[n];//pre[i]表示索引为i的字符的前驱字符所在位置
		for(int i=0;i<26;i++){
			last[i]=-1;//此数组存储的是字符a等的索引值。作为一个临时变量
		}
		//存储字符串中每个字符的前驱
		for(int i=0;i<n;i++){
			int x=s.charAt(i)-'a';
			pre[i]=last[x];//i=0处为a,则x=0,a的前驱索引为-1,假如i=3时为第2个a,此时该a的前驱last[0]=0
			last[x]=i;
		}
		
		int ans=0;//记录符合条件的子串的个数,这个值即为本题最终结果
		for(int i=0;i<n;i++){
			//深刻理解上面得分字符的含义。
			ans+=(i-pre[i])*(n-i);//此处当pre[i]=-1,说明i无前驱为i+1
		}
		System.out.println(ans);
	}
}

测试用例如下

ababc
28
aaa
6
aabbcc
37
afdjsfksjsbkdjfisfjsiofalfjsidfiwefjksfjsdifjsaflkjsdfasodfjslfjsdkfjadfjdskfalsfjdslfjskdfjsldfjsdkfjslfasdfwiefsdfsofdd
65447
此处没法验证这个字符串的子串分值和到底对不对,太多啦。

在这里插入图片描述
【输入格式】
第一行包含一个整数N
第二行包含N个整数A1,A2,…,An
【输出格式】
输出N个整数,依次是最终的A1,A2,…,An
【样例输入】
5
2 1 1 3 4
【样例输出】
2 1 3 4 5
对于 80% 的评测用例,1 ≤ N ≤ 10000。
对于所有评测用例,1 ≤ N ≤ 100000,1 ≤ Ai ≤ 1000000。
在这里插入图片描述
思路1时间复杂度可达O(10^11),显然要进行优化。
在这里插入图片描述
在这里插入图片描述
解释上述代码含义:set是数据结构,背后是平衡树原理。make_pair()生成数对。生成两个边缘数对和(a[0],a[0])数对,此后遍历a[0]其后每一个元素,得到两个区间itr和pre.
upper_bound(make_pair(a[i],INF))生成一个区间,其左端点是大于a[i]的最小数。pre即为itr相邻的前一个区间,其左端点数小于或等于a[i].如果pre的右端点大于等于a[i],则a[i]在pre区间中,a[i]值直接变为pre-second+1.后面是处理区间合并的四种情况,画图如下
在这里插入图片描述
其他情况类似如上图所示或左接或右接,或单独一个数形成区间(左右端点值相同)

java 代码如下:

第一种解法:
TreeSet 的add和remove方法是根据比较器来进行的。否则不能添加和删除元素。比较器即确定了元素的相等和大小关系。TreeSet集合中存储的元素是不重复的有序的元素。我们使用Pair来维护区间。将此区间放入TreeSet集合中。

package com.mike.alice;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

/**
 * 
 * @Description
 * @author mike
 * @version
 * @date 2021-3-15下午2:42:40
 *
 */
public class Main5 {

	static Set<Pair> treeSet=new TreeSet<>();
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		int n=scanner.nextInt();
//		System.out.println(n);
		int[] a=new int[n];
		for(int i=0;i<n;i++){
			a[i]=scanner.nextInt();
		}
		treeSet.add(new Pair(Integer.MIN_VALUE,Integer.MIN_VALUE));
		treeSet.add(new Pair(Integer.MAX_VALUE,Integer.MAX_VALUE));
		treeSet.add(new Pair(a[0],a[0]));
//		Iterator<Pair> iterator=treeSet.iterator();
//		while (iterator.hasNext()) {
//			Pair pair = (Pair) iterator.next();
//			System.out.println(pair);
//		}
		
		for (int i = 1; i < n; i++) {
			Pair pair=upperBound(treeSet, new Pair(a[i], Integer.MAX_VALUE));
			Pair pre=lowerBoundMax(treeSet, new Pair(a[i], Integer.MAX_VALUE));
			if (pre.second>=a[i]) {
				a[i]=pre.second+1;
			}
			//四种区间合并
			if (pre.second+1==a[i]&&pair.first-1==a[i]) {
				treeSet.add(new Pair(pre.first, pair.second));
				treeSet.remove(pair);
				treeSet.remove(pre);
			}else if (pre.second+1==a[i]) {
				treeSet.add(new Pair(pre.first, pre.second+1));
				treeSet.remove(pre);//TreeSet的删除与插入跟比较器有关。
			}else if (pair.first-1==a[i]) {
				treeSet.add(new Pair(pair.first-1, pair.second));
				treeSet.remove(pair);
			}else{
				treeSet.add(new Pair(a[i], a[i]));
			}
			
		}
		for (int i = 0; i < a.length; i++) {
			System.out.print(a[i]+" ");
		}
		System.out.println(pair);
		System.out.println(temp);
//		Iterator<Pair> it=treeSet.iterator();
//		while (it.hasNext()) {
//			Pair type = (Pair) it.next();
//			System.out.println(type);
//		}
		
	}
	
	//求大于[ai,+&]的第一个区间
	public static Pair upperBound(Set<Pair> treeSet,Pair pair){
		Iterator<Pair> iterator=treeSet.iterator();
		while (iterator.hasNext()) {
			Pair p = (Pair) iterator.next();
			if (p.first>pair.first) {
				return p;
			}
		}
		return null;
	}
	
	//求大于[ai,+&]的第一个区间的上一个区间
	public static Pair lowerBoundMax(Set<Pair> treeSet,Pair pair){
		Iterator<Pair> iterator=treeSet.iterator();
		Pair temp=null;
		while (iterator.hasNext()) {
			Pair p = (Pair) iterator.next();
			if (p.first<=pair.first) {
				temp=p;
			}
		}
		return temp;
	}
}

class Pair implements Comparable<Pair>{
	public int first;
	public int second;
	public Pair(int first,int second){
		this.first=first;
		this.second=second;
	}
	@Override
	public int compareTo(Pair o) {//升序排序
		if (first>o.first) {
			return 1;
		}else if(first<o.first){
			return -1;
		}else{
			if (second>o.second) {
				return 1;
			}else if (second<o.second) {
				return -1;
			}else{
				return 0;
			}
			
		}
	}
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "["+first+","+second+"]";
	}
}

测试用例如下:(此用例对下面的几种解法均适用,一秒出结果)

1000
1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5 1 2 3 100 5
1 2 3 100 5 4 6 7 101 8 9 10 11 102 12 13 14 15 103 16 17 18 19 104 20 21 22 23 105 24 25 26 27 106 28 29 30 31 107 32 33 34 35 108 36 37 38 39 109 40 41 42 43 110 44 45 46 47 111 48 49 50 51 112 52 53 54 55 113 56 57 58 59 114 60 61 62 63 115 64 65 66 67 116 68 69 70 71 117 72 73 74 75 118 76 77 78 79 119 80 81 82 83 120 84 85 86 87 121 88 89 90 91 122 92 93 94 95 123 96 97 98 99 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 

第二种解法是并查集更简单:
含义:并查集,在一些有N个元素的集合应用问题中,我们通常是在开始时让每个元素构成一个单元素的集合,然后按一定顺序将属于同一组的元素所在的集合合并,其间要反复查找一个元素在哪个集合中。并查集是一种树型的数据结构,用于处理一些不相交集合(disjoint sets)的合并及查询问题。常常在使用中以森林来表示。
主要步骤

  • 初始化

把每个点所在集合初始化为其自身。
通常来说,这个步骤在每次使用该数据结构时只需要执行一次,无论何种实现方式,时间复杂度均为O(N)。

  • 查找

查找元素所在的集合,即根节点。

  • 合并

将两个元素所在的集合合并为一个集合。
通常来说,合并之前,应先判断两个元素是否属于同一集合,这可用上面的“查找”操作实现。

package com.mike.alice;

import java.util.Arrays;
import java.util.Scanner;

/**
 * 
 * @Description
 * @author mike
 * @version
 * @date 2021-3-14下午8:28:44
 *
 */
public class Main3 {

	static int[] f=new int[2000000];
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		//获取第一行数据
		int n=Integer.parseInt(scanner.nextLine());
//		System.out.println(n);
		int[] data=new int[n];//A数组A1,A2等
		
		//初始化f数组
		for(int i=1;i<f.length;i++){
			f[i]=i;//并查集初始化,每个元素都是一个集合,它为此集合的代表,即根节点
		}
		
		//获取第二行数据,放到数组中
		for(int i=0;i<data.length;i++){
			data[i]=scanner.nextInt();
		}
//		System.out.println(Arrays.toString(data));
		
		for(int i=0;i<data.length;i++){
			int k=find(data[i]);
			data[i]=k;//data[i]元素用过啦就要加1
			f[data[i]]=find(data[i]+1);
		}
		for(int i=0;i<data.length;i++){
			System.out.print(data[i]+" ");
		}
		
	}
	//并查集
	public static int find(int x){//寻找x所在的集合,即根节点,f[x]为x的父节点
		if (x==f[x]) {
			return x;
		}else{
			f[x]=find(f[x]);
			return f[x];
		}
	}
}

测试用例如下:

5
2 1 1 3 4
2 1 3 4 5 

第三种解法:

  • 实质是类似哈希表存储里的: index+1探测
  • 建一个visited访问数组,记录访问的次数,访问次数跟index+1探测有直接关系,访问 i 次即直接令输入值加上i -1;
  • 每输入一个值,就查询visited数组
  • 如果有重复,将值按探测方式更新
  • 把值存入答案sto栈中

代码如下:

package com.mike.alice;

import java.util.Scanner;

/**
 * 
 * @Description
 * @author mike
 * @version
 * @date 2021-3-14下午8:39:55
 *
 */
public class Main4 {

	static Scanner scanner=new Scanner(System.in);
	static int MAX=2000000;
	static int[] vis=new int[MAX];//记录访问的次数
	static int[] sto=new int[MAX];//数组
	static int top=0;//记录指针
	public static void main(String[] args) {
		int n=scanner.nextInt();
		for(int i=1;i<=n;i++){
			int va=scanner.nextInt();
			while(vis[va]!=0){
				vis[va]++;//记录访问va的次数
				va+=(vis[va]-1);//va加上其访问次数-1,若访问次数为0,说明此数va未在前面访问过
			}
			vis[va]++;//访问一次,加一次
			sto[top++]=va;
		}
		
		for(int i=0;i<top;i++){
			System.out.print(sto[i]+" ");
		}
	}
}

测试用例如下

10
2 1  1  3   3   3  32 2  2 5
2 1 3 4 5 6 32 7 8 9  

感受:将C++语言转化成Java语言,真难。这让我明白Java的一些常用数据结构我仍然不甚熟悉。两重循环,对其中一重循环加以优化,其时间大大了减小啦。

在这里插入图片描述
此题简单直接代码如下:

package com.mike.alice;

import java.util.Scanner;

/**
 * 
 * @Description
 * @author mike
 * @version
 * @date 2021-3-15下午3:37:34
 *
 */
public class Main6 {

	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		int n=scanner.nextInt();//记录考试人数
		int[] score=new int[n];//此数组记录学生的分数
		int count1=0;//记录及格人数
		int count2=0;//记录优秀人数
		for (int i = 0; i < score.length; i++) {
			score[i]=scanner.nextInt();
		}
		for (int i = 0; i < score.length; i++) {
			if (score[i]>=85) {
				count2++;
			}
			if (score[i]>=60) {
				count1++;
			}
		}
		System.out.println(Math.round(count1*100.0/n)+"%");
		System.out.println(Math.round(count2*100.0/n)+"%");
		
	}
}

Math.round(double a)四舍五入取整,即返回与a的值最相近的长整型数。
测试用例如下:

3
12
70
89
67%
33%

在这里插入图片描述
在这里插入图片描述

package com.mike.alice;

import java.util.Scanner;

/**
 * 
 * @Description
 * @author mike
 * @version
 * @date 2021-3-15下午3:54:08
 *
 */
public class Main7 {

	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		String str=scanner.nextLine();
		int[] count=new int[26];//索引0代表a,索引25代表z,记录字符出现的次数
		for (int i = 0; i < count.length; i++) {
			count[i]=0;//初始化
		}
		for (int i = 0; i < str.length(); i++) {
			count[str.charAt(i)-'a']++;
		}
		int max=0;//记录索引
		int maxValue=0;//记录值
		for (int i = 0; i < count.length; i++) {
			if (count[i]>maxValue) {
				maxValue=count[i];
				max=i;
			}
		}
		System.out.println((char)(max+'a'));
		System.out.println(maxValue);
	}
}

测试用例如下:

longlong
g
2
longlongtooloto
o
6

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

package com.mike.alice;

import java.util.Scanner;

/**
 * 
 * @Description
 * @author mike
 * @version
 * @date 2021-3-15下午4:54:39
 *
 */
public class Main8 {

	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		int n=scanner.nextInt();//表示三角形的行数
		int[][] a=new int[n][n];//记录三角形的数
		int[][] f=new int[n][n];//确定终点,记录到此终点路径的和
		for (int i = 0; i < n; i++) {
			for (int j = 0; j <=i ; j++) {
				a[i][j]=scanner.nextInt();
			}
		}
		f[0][0]=a[0][0];//到三角形顶点的最大的和
		for(int i=1;i<n;i++){
			for (int j = 0; j <= i; j++) {
				if (j==0) {//最左边,只有一个方向
					f[i][j]=f[i-1][j]+a[i][j];
				}else if (j==i) {//最右边,只有一个方向
					f[i][j]=f[i-1][j-1]+a[i][j];
				}else{//两个方向,取最大的
					f[i][j]=Math.max(f[i-1][j], f[i-1][j-1])+a[i][j];
				}
			}
		}
		
		//上面求f[i][j]从a[0][0]到点[i,j]路径和的最大值,不考虑左右相差次数
		//中间一个或两个点位置确定后就会导致左右走的次数相差不超过1
		if (n%2==1) {//中间一个点
			System.out.println(f[n-1][n/2]);
		}else {//中间两个点取最大值
			System.out.println(Math.max(f[n-1][n/2-1], f[n-1][n/2]));
		}
		
	}
}

测试用例如下:

5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
27
4
7
3 8
8 1 0
2 7 4 4
25

本科组的题目还是挺难的,高职高专组的题目简单的多。不管怎样,简单的实现一定要亲手敲代码训练,要不然的你懂的。手速跟不上思想。请看到的同学不吝赐教,批评改正。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值