UVa1062
来源:互联网 发布:淘宝装修消除模块间距 编辑:程序博客网 时间:2024/06/03 21:53
题意:给出一个字符串,问需要几个栈,来使得每个栈的字符串按字典序方式存在栈中
思路:在遍历字符串时,在当前的几个栈中找到满足当前字符小于栈顶字符,并且与栈顶字符距离最近。
代码如下:
import java.io.BufferedInputStream;import java.util.Arrays;import java.util.Scanner;public class Main { private int solver(String s) { char[] chars = s.toCharArray(); int[] stacks = new int[chars.length]; int cnt = 0; for (int i = 0; i < chars.length; i++) { boolean found = false; int ch = chars[i] - 'A' + 1; int max = Integer.MAX_VALUE; int curpos = 0; for (int j = 0; j < cnt; j++) { if (stacks[j] >= ch) { found = true; if (stacks[j] - ch < max) { max = stacks[j] - ch; curpos = j; } } } if (found) { stacks[curpos] = ch; } else { stacks[cnt++] = ch; } } return cnt; } public static void main(String[] args) { Scanner scanner = new Scanner(new BufferedInputStream(System.in)); Main solver = new Main(); int cas = 1; while (scanner.hasNext()) { String s = scanner.nextLine(); if ("end".compareTo(s) == 0) break; int ans = solver.solver(s); System.out.println("Case " + cas++ + ": " + ans); } }}
阅读全文
0 0
- UVa1062
- JVM -XX: 参数介绍
- 自定义RecyclerView实现QQ侧滑删除按钮
- 编译时报错: error: <strong>two or more data types in declaration specifiers</strong> error:two or more
- MYSQL-创建数据表
- 常见网络攻击方式
- UVa1062
- [iOS]系统TabBar简单使用
- c拾遗
- 震网三代在metasploit-framework上的复现与利用
- 浅谈机器学习用来做什么,它是如果工作的
- iOS之《Effective Objective-C 2.0》读书笔记(17)
- Java设计模式-单例模式及线程安全问题
- js验证带小数点的数字
- 百万级开源MQTT消息服务器 搭建