26 March 2018

单种子连续加密数求解

	public static long[] getTempSeeds(String[] words) {
		int SIZE = words.length;
		long[] rs = new long[SIZE];
		boolean allEql = true;
		for (int j = 0; j < SIZE && allEql; j++) {
			String curWord = words[j];
			char[] chrs = curWord.toCharArray();
			boolean thisEql = false;
			for (int i = 1; i < Integer.MAX_VALUE && !thisEql; i++) {
				Random ran = new Random(i);
				// Add recheck function
				boolean wdEql = true;
				boolean shouldContinue = true;
				StringBuffer ranStr = new StringBuffer();
				do {
					wdEql = true;
					for (int k = 0; k < chrs.length && wdEql; k++) {
						int ka = ran.nextInt(27);
						if (ka == 0) {
							break;
						}
						char ch = (char) ('`' + ka);
						ranStr.append(ch);
						wdEql &= (chrs[k] == ch);
					}
					int limit = ranStr.length();
					if (!wdEql) {
						if (limit > chrs.length) {
							// search if ranStr contains pattern
							String tails = ranStr.substring(limit - chrs.length - 1, limit);
							for (int m = 0; m < tails.length(); m++) {
								boolean isPatternFound = true;
								int curpos = m;
								int n = 0;
								for (; n < chrs.length && curpos < chrs.length && isPatternFound; n++) {
									if (tails.charAt(curpos) == chrs[n]) {
										curpos++;
										continue;
									}
									else {
										isPatternFound = false;
									}
								}

								if (isPatternFound) {
									// If found, stop progress
									if (n == chrs.length) {
										if (curpos > m) {
											shouldContinue = false;
											wdEql = true;
											System.out.println(limit);
											System.out.println(tails);
											break;
										}
									}
								}
								else {
									if (curpos > m) {
										m = curpos - 1;
									}
									continue;
								}
							}
						}
					}
				}
				while (shouldContinue);
				if (wdEql) {
					rs[j] = i;
					System.out.println(i);
				}
				thisEql |= wdEql;
			}
			allEql &= thisEql;
		}
		return rs;

	}

单词头匹配种子加密数求解

	public static long[] getTempSeedsOnce(String[] words) {
		int SIZE = words.length;
		long[] rs = new long[SIZE];
		boolean allEql = true;
		for (int j = 0; j < SIZE && allEql; j++) {
			String curWord = words[j];
			char[] chrs = curWord.toCharArray();
			boolean thisEql = false;
			for (int i = 1; i < Integer.MAX_VALUE && !thisEql; i++) {
				Random ran = new Random(i);
				// Add recheck function

				boolean wdEql = true;
				StringBuffer ranStr = new StringBuffer();
				for (int k = 0; k < chrs.length && wdEql; k++) {
					int ka = ran.nextInt(27);
					char ch = (char) ('`' + ka);
					ranStr.append(ch);
					wdEql &= (chrs[k] == ch);
				}
				if (wdEql) {
					rs[j] = i;
					System.out.println(ranStr);
					System.out.println(i);
				}
				thisEql |= wdEql;
			}
			allEql &= thisEql;
		}
		return rs;
	}

测试用例

	public static void main(String[] args) {
		String[] words = "hello world".split(" ");
		getTempSeedsOnce(words);
		getTempSeeds(words);
	}

}


blog comments powered by Disqus
Flag Counter