The Cue Programming Challenge


  Level 1

  ----------------------------------------

  Embedded in this block of text is the password for level 2.
  The password is the longest substring that is the same in reverse.

  As an example, if the input was "I like racecars that go fast"
  the password would be "racecar".

  Enter the password to access level 2: 
this block of text
FourscoreandsevenyearsagoourfaathersbroughtforthonthiscontainentanewnationconceivedinzLibertyanddedicatedtothepropositionthatallmenarecreatedequalNowweareengagedinagreahtcivilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth
分析
就是最长回文子串的查找,看以参见以前的文章http://blog.youkuaiyun.com/perfect8886/article/details/9293913,有O(n)的解法。
 
 
  Level 2

  ----------------------------------------

  Congratulations.  You have reached level 2.

  To get the password for level 3, write code to find the first prime
  fibonacci number larger than a given minimum.  For example, the first
  prime fibonacci number larger than 10 is 13.

  When you are ready, go here or call this automated
  number (415) 799-9454.

  You will receive additional instructions at that time.  For the second portion
  of this task, note that for the number 12 we consider the sum of the prime divisors
  to be 2 + 3 = 5.  We do not include 2 twice even though it divides 12 twice.

  Enter the password to access level 3: 
go here
Step 1. Use your code to compute the smallest prime fibonacci number
	        greater than 227,000.  Call this number X.
	
	Step 2. The password for level 3 is the sum of prime divisors of X + 1.
	
	Note: If you call the number instead, it will check your answer for step 1.
分析
首先是获取质数,用筛选法获取1000000以内的质数就够用了,针对筛选法还有很多优化方法,可以参考http://blog.youkuaiyun.com/chengyingzhilian/article/details/7314851里面说的优化方法,但是这篇文章里的程序是有问题的,最好自己动手实现一下。
然后是斐波那契额递推数列,正常递推即可。
最后就是质因数分解,短除法即可。
解答
public class FibonacciPrimes {
	private static final int N = 1000000;
	private boolean[] prime = new boolean[N + 1];

	private void getPrimes() {
		for (int i = 2; i < N; ++i) {
			prime[i] = true;
		}
		for (int i = 2; i <= Math.sqrt(N); ++i) {
			if (prime[i]) {
				for (int j = i + i; j < N; j += i) {
					prime[j] = false;
				}
			}
		}
	}

	private int p = 1;

	private int getNextPrime() {
		++p;
		while (!prime[p]) {
			++p;
		}
		return p;
	}

	private int getFibonacci(int t) {
		getPrimes();
		int a = 1;
		int b = 1;
		while (true) {
			int c = a + b;
			if (c > t && prime[c]) {
				return c;
			}
			a = b;
			b = c;
		}
	}

	private int getPrimeDivesSum(int x) {
		int sum = 0;
		p = 1;
		while (x != 1) {
			int f = getNextPrime();
			if (x % f == 0) {
				sum += f;
				while (x % f == 0) {
					x /= f;
				}
			}
		}
		return sum;
	}

	public static void main(String[] args) {
		FibonacciPrimes fp = new FibonacciPrimes();
		int x = fp.getFibonacci(227000);
		System.out.println(x);
		System.out.println(fp.getPrimeDivesSum(x + 1));
	}
}

  Level 3

  ----------------------------------------

  Congratulations.  You have reached the final level.

  For the final task, you must find all subsets of an array
  where the largest number is the sum of the remaining numbers.
  For example, for an input of:

  (1, 2, 3, 4, 6)

  the subsets would be

  1 + 2 = 3
  1 + 3 = 4
  2 + 4 = 6
  1 + 2 + 3 = 6

  Here is the list of numbers you should run your code on.
  The password is the number of subsets.  In the above case the
  answer would be 4.

  Enter the password to complete the challenge: 
list of numbers
3, 4, 9, 14, 15, 19, 28, 37, 47, 50, 54, 56, 59, 61, 70, 73, 78, 81, 92, 95, 97, 99
分析
最原始的作法就是:暴利枚举所有组合,进行统计。针对该题的小数量级是可行的,大数量级就无法容忍了。
另一种作法就是:O(n*max)的动态规划,n为数组长度,max为数组最大值。通过公式D[sum] += D[sum - a[i]]不断累加所有的可能组合。
解答
public class SubsetSums {
	private final static int[] a = { 3, 4, 9, 14, 15, 19, 28, 37, 47, 50, 54,
			56, 59, 61, 70, 73, 78, 81, 92, 95, 97, 99 };

	public static void main(String[] args) {
		int ret = 0;
		int N = a.length;
		int max = a[N - 1];
		int[] D = new int[max + 1]; // index: sum; value: count
		D[0] = 1;
		for (int i = 0; i < N; ++i) {
			ret += D[a[i]];
			for (int j = max - a[i]; j >= 0; --j) {
				D[j + a[i]] += D[j];
			}
		}
		System.out.println(ret);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值