TopCoder SRM144 DIV1(one)

 1 import java.util.Scanner;

 2 

 3 public class BinaryCode {

 4     

 5     public String[] decode(String message){

 6         String[] original =  new String[2];

 7         StringBuffer s1=new StringBuffer();

 8         StringBuffer s2=new StringBuffer();

 9         

10         int flag1=0,flag2=0;

11         char temp;

12         

13         for(int i=1;i<message.length();i++){

14             if(i==1)

15             {

16                 if(flag1==0)

17                 {

18                     s1.insert(0, '0');

19                     temp = (char) (message.charAt(0)-s1.charAt(0)+'0');

20                     if(temp!='0'&&temp!='1')

21                         flag1=1;

22                     s1.insert(1,temp);

23                 }

24                 

25                 if(flag2==0)

26                 {

27                     s2.insert(0, '1');

28                     temp = (char) (message.charAt(0)-s2.charAt(0)+'0');

29                     if(temp!='0'&&temp!='1')

30                         flag2=1;

31                     s2.insert(1, temp);

32                 }

33             }

34             else{

35                 

36                 if(flag1==0)

37                 {

38                     temp = (char) (message.charAt(i-1)-s1.charAt(i-1)+'0'-s1.charAt(i-2)+'0');

39                     if(temp!='0'&&temp!='1')

40                         flag1=1;

41                     s1.insert(i,temp);

42                 }

43                 

44                 if(flag2==0)

45                 {

46                     temp = (char) ((message.charAt(i-1)-'0')-(s2.charAt(i-1)-'0')-(s2.charAt(i-2)-'0')+'0');

47                     if(temp!='0'&&temp!='1')

48                         flag2=1;

49                     s2.append(temp);

50                 }

51             }

52             

53             if(flag1==1&&flag2==1){

54                 break;

55             }

56             //System.out.println(i+1+":"+s1+","+s2);

57         }

58         

59         if(flag1==0)

60             if(message.charAt(message.length()-1)!=s1.charAt(message.length()-1)+s1.charAt(message.length()-2)-'0')

61                 flag1=1;

62         if(flag2==0)

63             if(message.charAt(message.length()-1)!=s2.charAt(message.length()-1)+s2.charAt(message.length()-2)-'0')

64                 flag2=1;

65             

66         if(flag1==1)

67             original[0]="NONE";

68         else

69             original[0]=s1.toString();

70         

71         if(flag2==1)

72             original[1]="NONE";

73         else

74             original[1]=s2.toString();

75             

76         

77         

78         return original;

79     }

80 

81     /**

82      * @param args

83      */

84     public static void main(String[] args) {

85         // TODO Auto-generated method stub

86         

87         BinaryCode bc=new BinaryCode();

88         Scanner cin=new Scanner(System.in);

89         while(cin.hasNext()){

90             String[] res=bc.decode(cin.next());

91                 System.out.println("{"+res[0]+","+res[1]+"}");

92         }

93     }

94 }

 

你可能感兴趣的:(topcoder)