一.子串分值和
给定一个字符串,求所有字串不同字符数量相加的总和
import java.util.Arrays;
import java.util.Scanner;
public class 子串分值和0311 {
static int[]last=new int[26];
public static void main(String[] args) {
//输入
Scanner sc=new Scanner(System.in);
String str=sc.next();
Arrays.fill(last,-1);
long sum=0;
for(int i=0;i<str.length();i++){
sum+=(long)(i-last[str.charAt(i)-'a'])*(str.length()-i);
last[str.charAt(i)-'a']=i;
}
System.out.println(sum);
}
}
暴力算法:求每个字串,再用str.contain();求每个字符是否在其中,最后求和。
算法:假设一个字符a在一个字串出现多次,我们只将最左边的一个当大头。
从第一个字符开始计算,一枚个字符为中心求所有包含它的字串。但是左极限不能超过上一个它,如..a....a......,如果轮到第二个a,则计算时不能包括第一个a及其之前的字符。最后求和就好
核心:1.不重复
2.一个字符串的子串是由不同的字符构成的,我们一个一个字符求这个字符的所有可能的子串,并且并且通过“相同字符以左边为大头”的做法,排除了重复的可能性,那么所有次数相加之和就是每个子串含有不同字符相加之和。如hsadfsajidhfiuoah 忘了就推一推
二.数位排序
输入一串数字,按数位和排序,数位和相同就按大小排序
import java.util.Scanner;
public class 数位排序0306 {
//1.按位数和排
//2.位数和相同的按大小排
public static void main(String[] args) {
//如果等于0就添加
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[][] arr = new int[58][100000];
int[] arr1 = new int[58];
for (int i = 1; i <= n; i++) {//放入
int a1 = he(i);
arr[a1][arr1[he(i)]] = i;
arr1[he(i)]++;
}
//输出
int si = 0;
for (int i = 1; i < 58; i++) {
for (int j = 0; j < 1000005; j++) {
if (arr[i][j] != 0) {
si++;
if (si == m) {
System.out.println(arr[i][j]);
return;
}
} else {
break;
}
}
}
}
public static int he(int i) {//计算位数和
int sum = 0;
while (i != 0) {
sum = sum + i % 10;
i = i / 10;
}
return sum;
}
}
先将求和的方法写出,按照顺序,根据求和的大小将数字依次放入数组(由于是位数和和大小两个指标,所以想到了用二维数组)(巧妙的用数组储存每个数位和当前的数量,作为指标可以直接将数字饭放入对应的位置),最后再依次输出。
3.楼层高度对比
输入楼数,再输入对应楼层高度
第一行输出所有楼左边第一栋比自己高的楼的坐标,没有就-1;
第二行输出所有楼右边第一栋比自己高的楼的坐标,没有就-1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
public class 楼层高度对比0311 {
//单调栈模板题
static Stack<Integer> st1 = new Stack<>();//存储栈底
static Stack<Integer> st2 = new Stack<>();//存储对应的序号
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(in.readLine());
String[] str = in.readLine().split(" ");
int[] sum = new int[str.length];
int[] arr = new int[str.length];//存储对应的数值
for (int i = 0; i < str.length; i++) {//每一栋楼
arr[i] = Integer.parseInt(str[i]);
if (i == str.length - 1) {
System.out.print(panduan(arr[i], str, i));
} else {
System.out.print(panduan(arr[i], str, i) + " ");
}
}
System.out.println();
st1.clear();
st2.clear();
for (int i = str.length - 1; i >= 0; i--) {//每一栋楼
sum[i] = panduan(arr[i], str, i);
}
for (int i = 0; i < str.length; i++) {
if (i == str.length - 1) {
System.out.print(sum[i]);
} else {
System.out.print(sum[i] + " ");
}
}
}
public static int panduan(int h, String[] str, int i) {
while (st1.size() != 0) {
if (h >= st1.peek()) {
st1.pop();
st2.pop();
} else {
int temp = st2.peek();
st1.push(h);
st2.push(i + 1);
return temp;
}
}
st1.push(h);
st2.push(i + 1);
return -1;
}
}
运用了java自带的栈Stack。因为一个一个往前对比,如果有一个数比前面的数大,那么比它小的数就永远不可能用的到。于是,一个个数开始进栈,如果比起那卖你的数大,则将小的数出栈直到遇到大数,然后次数进栈同时在另一个栈里也土壤对应的序号进栈。这两个栈用来快速的得出大的楼的坐标。(算是一种淘汰思想吧,结合栈进栈出栈)。
栈的方法:获取顶部st.peed(); 获取并移除顶部st.pop(); 添加元素st.push();
4.MAX最值差
运用了Duque双端队列两端,均可进可出。
5.compareTo
class Good implements Comparable<Good> {
long price;
long date;
long count;
public Good(long price, long date, long count) {
this.price = price;
this.date = date;
this.count = count;
}
@Override
public int compareTo(Good o) {
return (int) (this.price - o.price);
}
}
设置顺序排放的规则
注意:用快读是,所有的输入都用快读,不然会出错