首页 > 专题系列 > Java解POJ > POJ 3128 Leonardo’s Notebook [解题报告] Java
2013
11-12

POJ 3128 Leonardo’s Notebook [解题报告] Java

Leonardo’s Notebook

问题描述 :

— I just bought Leonardo’s secret notebook! Rare object collector Stan Ucker was really agitated but his friend, special investigator Sarah Kepticwas unimpressed.

— How do you know it is genuine?

— Oh, it must be, at that price. And it is written in the da Vinci code. Sarah browsed a few of the pages. It was obvious to her that the code was a substitution cipher, where each letter of the alphabet had been substituted by another letter.

— Leonardo would have written the plain-text and left it to his assistant to encrypt, she said. And he must have supplied the substitution alphabet to be used. If we are lucky, we can find it on the back cover! She turned up the last page and, lo and behold, there was a single line of all 26 letters of the alphabet:

QWERTYUIOPASDFGHJKLZXCVBNM

— This may be Leonardo’s instructions meaning that each A in the plain-text was to be replaced by Q, each B withW, etcetera. Let us see… To their disappointment, they soon saw that this could not be the substitution that was used in the book. Suddenly, Stan brightened.

— Maybe Leonardo really wrote the substitution alphabet on the last page, and by mistake his assistant coded that line as he had coded the rest of the book. So the line we have here is the result of applying some permutation TWICE to the ordinary alphabet! Sarah took out her laptop computer and coded fiercely for a few minutes. Then she turned to Stan with a sympathetic expression.

— No, that couldn’t be it. I am afraid that you have been duped again, my friend. In all probability, the book is a fake.

Write a program that takes a permutation of the English alphabet as input and decides if it may be the result of performing some permutation twice.

输入:

The input begins with a positive number on a line of its own telling the number of test cases (at most 500). Then for each test case there is one line containing a permutation of the 26 capital letters of the English alphabet.

输出:

For each test case, output one line containing Yes if the given permutation can result from applying some permutation twice on the original alphabet string ABC…XYZ, otherwise output No.

样例输入:

2
QWERTYUIOPASDFGHJKLZXCVBNM
ABCDEFGHIJKLMNOPQRSTUVWXYZ

样例输出:

No
Yes

解题代码:

/* @author: */
import java.util.Scanner;
public class Main {
 public static void main(String[] args) {
  Scanner sc = new Scanner(System.in);
 
  int t, i, c, j;
  char w[]=new char[100];
  boolean s[]=new boolean[100];
  int ans[]=new int[100];
  t=sc.nextInt();
  while(( t-- )!=0) {
     w=sc.next().toCharArray();
     for( i=0; i< 26; i++ ) {
	  s[i] = false;
	  w[i]-='A';
	  ans[i+1] = 0;
     }

     for( i=0; i< 26; i++ ) {
	if( !s[i] ) {
	   s[i] = true;
	   for( j=w[i], c=1; j!=i; j=w[j], c++ )
		s[j] = true;
	   if( (c&1)==0 )
		ans[c]++;
	}
     }
     for( i=2; i<=26; i++ )
	if( (ans[i] & 1)!=0 )
	   break;
	System.out.printf( "%s\n", i<=26?"No":"Yes" );
   }
  }
}