URAL1654 也是一个典型的栈的问题,只不过这次进栈的是字母而不是HDU1022的火车了
http://acm.timus.ru/problem.aspx?space=1&num=1654
这个题的代码技巧比较高,如果写得妙的话,短短几行就可搞定,当然需要基本功扎实。比如说,这道题首先想到的是用Stack类,然后运用StringBuffer类进行输出,其中的几个方法功能、传入参数、返回值一定要记熟,用起来才游刃有余,如
Ⅰ.append(Object o) 将o追击到此序列,返回StringBuffer类
Ⅱ.charAt(int index) 返回此序列中指定索引处的char值
Ⅲ.indexOf(String str) 返回第一次出现的指定子字符串在该字符串中的索引(int型)
Ⅳ.reverse() 将此字符序列用其反转形式取代
Ⅴ.substring(int start,int end) 返回一个新的 String,它包含此序列当前所包含的字符 子序列。
Ⅵ.toString() 返回此序列中数据的字符串表示形式
当然还有好多方法,多查API文档,用多了就会记住
具体代码如下:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
String in=scan.nextLine();
Stack<Character> st=new Stack<Character>();
st.push(in.charAt(0));
for(int i=1;i<in.length();i++)
{
if(!st.empty()&&st.peek()==in.charAt(i))
{
st.pop();
}
else
{
st.push(in.charAt(i));
}
}
StringBuffer str=new StringBuffer("");
while(!st.empty()){
str.append(st.pop());//append()将栈顶值追加到此序列
}
System.out.println(str.reverse().subSequence(0, str.length()));
}
}
分享到:
相关推荐
Ural解题思路 Ural解题思路 Ural解题思路 Ural解题思路
Ural
URAL3D
Pascal acm_timus_ural_1148.pas
URAL(Timus Online Judge)部分测试数据 不全
Ural 1238 源代码 涉及算法(动态规划、贪心、DFS)
Pascal acm_timus_ural_1099.pas
ural 题解 yuhch123。 关于yuhch123: ioi2008 金牌第一名,这是当初它做ural 第一卷时的题解
包含了ural题库中Vol_I 到Vol_III的所有题目的解题思路
部分题解 大牛出品 Vol1-3 不是很全,约有200题左右
URAL-PHA
Ural ACM 1000源代码(c++),vc++6.0在XPsp2下编译通过,Timus Online Judge再线测评通过
因为大三了 不弄ACM了 所以这些就删了 删之前希望可以帮到别人
资源分类:Python库 所属语言:Python 资源全名:ural-0.28.0.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
大量线段树题目 zoj 1610 线段覆盖 poj 2777 线段覆盖 poj 2528 需要离散化,建树不同,需要处理不同->...ural 1019 覆盖加统计最长同一个颜色 zoj 2301 和上一题差不多,但是这个染色染的是点,注意染色为空的状况
基于张量类模板的线性代数C ++库,可用于表示等级0的张量,标量向量,1的向量,2的矩阵,3的等级3的张量等。它还包括BLAS和LAPACK子例程的接口。
28. The Theoretical Background of the Ural-Altale Hypothesis The hypothesis of common origin of the Altaic languages,and even the Ural-Altaic languages,dates from the first half of the last century....
乌拉尔
Philip_Dural_UX-UI_Designer:UXUI设计器配置文件
资源分类:Python库 所属语言:Python 资源全名:ural-0.25.0-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059