首页 > 专题系列 > Java解POJ > POJ 3510 A Tale from the Dark Side of the Moon [解题报告] Java
2013
11-12

POJ 3510 A Tale from the Dark Side of the Moon [解题报告] Java

A Tale from the Dark Side of the Moon

问题描述 :

Us: So why don’t you just recompile the program on the new hardware?

Them: We cannot. We lost the source code.

Us: How typical! What does the program do? Do you have any documentation?

Them: The manual page does mention something about the documentation in the source code.

Us: A manual page is good. What does it say? Them: Just one line: “See the source code for more information.”

Us: Argh! What do you know about the program?

Them: Well, it seems to be taking simple text, similar to that found in an English dictionary, and printing it after some modification.

Us: What kind of modification?

Them: It removes any character that is not a lowercase letter. But not white spaces. White spaces are preserved as seen in the input.

Us: Do you have a sample input/output?

Them: Plenty. Here’s one. (see next section.)

Us: This is rather small! Did you try it on anything bigger?

Them: It works on any text as long as the lines are less then eighty characters wide. It doesn’t seem to mind working on lengthy documents. But it does terminate once it sees the sequence "EOF" (without the double quotes.)

One of them: Don’t forget to tell them about the "dd" thingy.

Us: What "dd" thingy?

Them: Whenever it sees a pair of small letter "d", one right after the other, it replaces them with "p".

Us: Why?

Them: Who knows? It just does that!

Us: What about "ddd" and "dddd"? How does it behave then?

Them: Where in English will you find a "dddd" or even a "ddd"? Haven’t you been listening?

Us: Oops. We’ll pay more attention. Anything else?

Another one of them: There is also the "vv" thingy.

Us: What about "vv"?

Them: Every "vv" is replaced with a "m".

Yet another one of them: No, wait! That was a printer problem. it had nothing to do with the program. Remember?

Them: Oh, that’s right. Forget about the "vv" thingy.

Us: What about the "dd" thingy? Was that just a printer problem too?

Them: No. That was the program.

Us: What else?

Them: One last thing. It seems to be replacing every "ei" with "ie".

Us: Every one of them?

Them: Except if it comes right after "c" then it remains as is.

Us: Makes sense. That’s all, right?

Them: No, we just remembered one more thing: It replaces the sequence "pink" with "floyd" anywhere in the text.

Us: What?! Who wrote this program? Why do you need it in the first place?

Them: We think it will increase our chances of going to Banff in April 2008 if we get it right.

Us: Yeah! Right.

<

Input

Output

>

样例输入:

unpinked is an 8 letter word. Honest!
vv is ok, d123d is ok, 123dd is not
i received mail from        liechtenstein
 ..  ...adding means to imitat.#$!%%$e
EOF

样例输出:

unfloyded is an  letter word onest
vv is ok dd is ok p is not
i received mail from        liechtenstien
   aping means to imitate

解题代码:

//* @author: [email protected]
import java.util.*;
public class Main
{
 public static void main(String[] args)
 {
  Scanner in=new Scanner(System.in);
  while(true)
  {
   String s=in.nextLine();
   StringBuffer sb=new StringBuffer("");
   int l=s.length();
   boolean bb=false;
   for(int i=0;i< l;i++)
   {
	char c=s.charAt(i);
	if(i< l-1&&c=='d'&&s.charAt(i+1)=='d'){
		sb.append('p');
		i++;
	}
	else if(i< l-1&&c=='e'&&s.charAt(i+1)=='i'&&(i==0||s.charAt(i-1)!='c')){
		sb.append("ie");
		i++;
	}			
	else if(i< l-3&&c=='p'&&s.charAt(i+1)=='i'&&s.charAt(i+2)=='n'&&s.charAt(i+3)=='k'){
		sb.append("floyd");
		i+=3;
	}	
	else if(i< l-2&&c=='E'&&s.charAt(i+1)=='O'&&s.charAt(i+2)=='F'){
		bb=true;
		break;
	}
	else if(c>='a'&&c<='z'||c==' ') sb.append(c);
    }
    System.out.println(sb);
    if(bb) break;
   }
 }
}

  1. 学算法中的数据结构学到一定程度会乐此不疲的,比如其中的2-3树,类似的红黑树,我甚至可以自己写个逻辑文件系统结构来。

  2. #include <stdio.h>
    int main()
    {
    int n,p,t[100]={1};
    for(int i=1;i<100;i++)
    t =i;
    while(scanf("%d",&n)&&n!=0){
    if(n==1)
    printf("Printing order for 1 pages:nSheet 1, front: Blank, 1n");
    else {
    if(n%4) p=n/4+1;
    else p=n/4;
    int q=4*p;
    printf("Printing order for %d pages:n",n);
    for(int i=0;i<p;i++){
    printf("Sheet %d, front: ",i+1);
    if(q>n) {printf("Blank, %dn",t[2*i+1]);}
    else {printf("%d, %dn",q,t[2*i+1]);}
    q–;//打印表前
    printf("Sheet %d, back : ",i+1);
    if(q>n) {printf("%d, Blankn",t[2*i+2]);}
    else {printf("%d, %dn",t[2*i+2],q);}
    q–;//打印表后
    }
    }
    }
    return 0;
    }