Uva 10935 10763 10391 JAVA题解

其实这几道题思路都很清晰 关键是算法的简洁和高效
因为这几道题都有超时 所以我觉得也需要记录一下
用了合适的数据结构,代码也不能再删减的情况下,你需要看看是不是你的思路本身就超时了,有没有更简单的呢,换个思路看看(例如10391 看了评论区大佬的答案,简洁又高效,我也希望下次做题的时候不只是求出个答案,而是达到更高的境界 :像是如何更好更快地求出这个答案
**

10935

/*还有一个TIP:QUEUE是一个接口 如果我们想使用单纯的queue而不是优先队列什么的 我们可以使用linklist因为它实现
QUEUE接口了的*/
public class Uva10935 {
public static void main(String[] args)
	{
	Scanner in = new Scanner(System.in);
	int m = in.nextInt();
    
    while(m!=0)
    {
    	
    	Queue<Integer> cards = new LinkedList<Integer>();
    	ArrayList<Integer> discard = new ArrayList<>();
    	
    	/*以下是处理部分( •̀ ω •́ )y*/
    	/*给牌编号*/
    	for(int i =1;i<=m;i++)
    	{
    		cards.add(i);
    	}
    	/*开始丢牌*/
    	System.out.print("Discarded cards:");
    	while(cards.size()>1)
    	{
    		int num = cards.poll();
    		System.out.print(" "+num);
    		if(cards.size()!=1)
    		System.out.print(",");
    		
    		/*discard.add(num);//丢弃第一张
*/    		cards.add(cards.poll());//将第二张 放入尾巴
    	}
    	System.out.println();
    	System.out.println("Remaining card: "+cards.peek());
    	/*输出部分*/

    	m = in.nextInt();
    }
    
}
}

10763

public class Uva10763 {
public static void main(String[] args) {
	Scanner in  = new Scanner(System.in);
	LinkedList<Integer> one = new LinkedList<>();
	LinkedList<Integer> two = new LinkedList<>();
	Map<Integer, ArrayList<Integer>> compare = new TreeMap<Integer, ArrayList<Integer>>();

	 long n = in.nextInt();
	 while(n!=0)
	 {
		 one.clear();
		 two.clear();
		for(int i =0;i<n;i++)
		 {
			 one.add(in.nextInt());
			 two.add(in.nextInt());
		 }
		 boolean flag = true;
		 Collections.sort(one);
		 Collections.sort(two);
		 if(one.equals(two))          //如果一一可以交换 则数字在俩个组出现的频率是一致的
			 System.out.println("YES");
		 else
			 System.out.println("NO");
			 		 n = in.nextInt();
	 }
}
}

10391

AC代码

public class Uva10391 {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
	   		String str  ="";
	   Set<String> dic = new TreeSet<>();
		/*全部读入 再进行处理*/
	   while(in.hasNextLine())
		{  
			str = in.nextLine();
			if(str.equals(""))
				str = in.nextLine();
			dic.add(str);
		}	   	  
	   for(String ss:dic) 
	   {
		   String left = "";
		   String right = "";		   
		   for(int i =1;i<ss.length();i++)
		   {
			   left = ss.substring(0,i);
			   right = ss.substring(i);
			   if(dic.contains(left)&&dic.contains(right))
			   {
				   System.out.println(ss);
				   break;
			   }
		   }
	   } 
	}

}

TLE原因分析;
全部读入后,逐一取出,与其他元素对比,将有可能的前后单词分别存入列表,
全部比较完后,再利用两重循环判断有可能的前后单词的长度加起来是否为取出的字符串长度。…循环用的太多了,难怪我TLE。看完AC代码,我觉得我不TLE谁不TLE,我的思路真的太一般了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值