字母重排

题目

输入一个字典,用*****结尾,然后再在一行中输入若干单词,回车结束。

每个单词w,都需要在字典中找出所有可以用w的字母重排后得到的单词,

并按照字典序从小到大的顺序在一行中输出。

如果不存在,输出:( 。

输入单词之间用空格或空行隔开。

注意,字典中的单词不一定按字典排列。


例子:

输入:tarp given score refund only trap work earn course pepper part
           ******
           resco nfudre aptr sett oresuc

输出:score 
           refund 
           tarp trap part 
           :(
           course 


解:

因为我是做完才看到从小到大的,我也懒得改了-_-!,所以我是按字典的输入顺序输出的。


public class Main {

	public static String fun(String s) {
		char[] c = s.toCharArray();
		Arrays.sort(c);
		return String.valueOf(c);
	}

	public static void main(String[] args) throws IOException {
		Scanner out = new Scanner(System.in);
		String[][] a = new String[2][50];
		int i = 0;
		do {
			String s = out.next();
			if (s.equals("******"))
				break;
			a[0][i] = s;
			a[1][i] = fun(s);
			i++;
		} while (true);
		out.nextLine();
		String s = out.nextLine();
		String[] b = s.split(" ");
		for (String ss : b) {
			ss = fun(ss);
			boolean is = false;
			for (int c = 0; c < a[0].length; c++) {
				if (ss.equals(a[1][c])) {
					is = true;
					System.out.print(a[0][c] + " ");
				}
			}
			if (!is) {
				System.out.println(":(");
			} else {
				System.out.println();
			}
		}
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值