POJ1248解题报告

  
Safecracker
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 1263 Accepted: 787

Description

"The item is locked in a Klein safe behind a painting in the second-floor library. Klein safes are extremely rare; most of them, along with Klein and his factory, were destroyed in World War II. Fortunately old Brumbaugh from research knew Klein's secrets and wrote them down before he died. A Klein safe has two distinguishing features: a combination lock that uses letters instead of numbers, and an engraved quotation on the door. A Klein quotation always contains between five and twelve distinct uppercase letters, usually at the beginning of sentences, and mentions one or more numbers. Five of the uppercase letters form the combination that opens the safe. By combining the digits from all the numbers in the appropriate way you get a numeric target. (The details of constructing the target number are classified.) To find the combination you must select five letters v, w, x, y, and z that satisfy the following equation, where each letter is replaced by its ordinal position in the alphabet (A=1, B=2, ..., Z=26). The combination is then vwxyz. If there is more than one solution then the combination is the one that is lexicographically greatest, i.e., the one that would appear last in a dictionary."

v - w 2 + x 3 - y 4 + z 5 = target

"For example, given target 1 and letter set ABCDEFGHIJKL, one possible solution is FIECB, since 6 - 9 2 + 5 3 - 3 4 + 2 5 = 1. There are actually several solutions in this case, and the combination turns out to be LKEBA. Klein thought it was safe to encode the combination within the engraving, because it could take months of effort to try all the possibilities even if you knew the secret. But of course computers didn't exist then."


"Develop a program to find Klein combinations in preparation for field deployment. Use standard test methodology as per departmental regulations.

Input

Input consists of one or more lines containing a positive integer target less than twelve million, a space, then at least five and at most twelve distinct uppercase letters. The last line will contain a target of zero and the letters END; this signals the end of the input.

Output

For each line output the unique Klein combination, or 'no solution' if there is no correct combination. Use the exact format shown below."

Sample Input

1 ABCDEFGHIJKL
11700519 ZAYEXIWOVU
3072997 SOUGHT
1234567 THEQUICKFROG
0 END

Sample Output

LKEBA
YOXUZ
GHOST
no solution
 
其实,用暴力的枚举法,只要字母不一样,且字母的组合出现在字典最后(字典最大),即为答案。
代码如下(为了最后一个即为字典最大,首先就需要对输入的字母进行排序):
import java.util.*;
/*
 * Kelein编码问题
 */
public class Main {

	//记录字母的顺序
	private HashMap<String,Integer> letter=new HashMap<String,Integer>();
	private HashMap<Integer,String> letter1=new HashMap<Integer,String>();
	
	//公式
	public long formula(int v,int w,int x,int y,int z)
	{
		long temp=(long) (v-Math.pow(w,2)+Math.pow(x,3)-Math.pow(y,4)+Math.pow(z,5));
		return temp;
	}
	
	public Main()
	{
		String[] strs={"","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
		for(int i=1;i<=26;i++)
		{
			letter1.put(i,strs[i]);
			letter.put(strs[i],i);
		}
		
		long num;
		String str;
		Scanner scan=new Scanner(System.in);
		while((num=Long.parseLong(scan.next()))!=0&&!(str=scan.next()).equals("END"))
		{
			char[] chs=str.toCharArray();
			
			//冒泡排序
			for(int i=0;i<chs.length-1;i++)
			{
				for(int j=0;j<chs.length-i-1;j++)
				{
					if(this.letter.get(String.valueOf(chs[j]))>this.letter.get(String.valueOf(chs[j+1])))
							{
								char temp=chs[j];
								chs[j]=chs[j+1];
								chs[j+1]=temp;
							}
				}
			}
			str="";
			for(int i=0;i<chs.length;i++)
			{
				str+=String.valueOf(chs[i]);
			}
			String[] result=new String[5];
			boolean tag=true;
		
			for(int v=0;v<chs.length;v++)
			{
				for(int w=0;w<chs.length;w++)
				{
					if(w==v)
					{
						continue;
					}
					for(int x=0;x<chs.length;x++)
					{
						if(x==v||x==w)
						{
							continue;
						}
						for(int y=0;y<chs.length;y++)
						{
							if(y==v||y==x||y==w)
							{
								continue;
							}
							for(int z=0;z<chs.length;z++)
							{
								if(z==v||z==w||z==x||z==y)
								{
									continue;
								}
								
								int vv=this.letter.get(String.valueOf(str.charAt(v)));
								int vw=this.letter.get(String.valueOf(str.charAt(w)));
								int vx=this.letter.get(String.valueOf(str.charAt(x)));
								int vy=this.letter.get(String.valueOf(str.charAt(y)));
								int vz=this.letter.get(String.valueOf(str.charAt(z)));
								
								if(num==this.formula(vv,vw,vx,vy,vz))
								{
								    	tag=false;
								    	result[0]=this.letter1.get(vv);
								    	result[1]=this.letter1.get(vw);
								    	result[2]=this.letter1.get(vx);
								    	result[3]=this.letter1.get(vy);
								    	result[4]=this.letter1.get(vz);
								}
							}
						}
					}
				}
			}
			if(tag)
			{
				System.out.println("no solution");
			}
			else
			{
				for(int i=0;i<5;i++)
				{
					System.out.print(result[i]);
				}
				System.out.println("");
			}
		}
	}
	
	public static void main(String[] args){
		
		Main mainf=new Main();
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值