AlgorithmicProblemSolvingStrategies / Study

AlgorithmicProblemSolvingStrategies
0 stars 0 forks source link

[2/15] Hamming Code #5

Open spdkimo opened 10 years ago

spdkimo commented 10 years ago

초심자용 문제 - 구현 http://algospot.com/judge/problem/read/HAMMINGCODE

문제 ID 시간 제한 메모리 제한 제출 횟수 정답 횟수 (비율) HAMMINGCODE 1000ms 65536kb 278 172 (61%)

Sprexatura commented 10 years ago

https://algospot.com/judge/submission/detail/213612 에 풀었습니다

def solve(inputString):
    check1 = int(inputString[0])^int(inputString[2])^int(inputString[4])^int(inputString[6])
    check2 = int(inputString[1])^int(inputString[2])^int(inputString[5])^int(inputString[6])
    check3 = int(inputString[3])^int(inputString[4])^int(inputString[5])^int(inputString[6])
    pos = check3*2**2 + check2*2 + check1
    if pos == 0: return inputString[2]+inputString[4]+inputString[5]+inputString[6]
    r = inputString[:pos-1] + str(int(inputString[pos-1]) ^  1) + inputString[pos:]
    return r[2]+r[4]+r[5]+r[6]

NumberOfTestcase = int(raw_input())
answerList = []
TestcaseNumber = 0
while NumberOfTestcase > TestcaseNumber:
    lineInput = raw_input().strip()
    answerList.append(solve(lineInput))
    TestcaseNumber = TestcaseNumber+1

for result in answerList: print result

solve 만 살피면 됩니다.

그냥 스팩대로 구현하면 통과하네요

spdkimo commented 10 years ago

import java.util.Scanner;

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int cases = sc.nextInt();
        while(cases-- > 0) {
            String sequence = sc.next();
            new Solve(sequence);
        }
        sc.close();
    }

    public static class Solve {
        Solve (String s) {
            byte[] bytes = new byte[7];
            for (int i = 0; i < 7; i++) {
                if (s.charAt(i) ==  '0')
                    bytes[i] = 0;
                else
                    bytes[i] = 1;
            }
            int syndrom = ((bytes[0]^bytes[2]^bytes[4]^bytes[6]) & 0x1) +
                    ((bytes[1]^bytes[2]^bytes[5]^bytes[6] & 0x1) * 2) +
                    ((bytes[3]^bytes[4]^bytes[5]^bytes[6] & 0x1) * 4);
            if (syndrom != 0) {
                bytes[syndrom - 1] = (byte)((bytes[syndrom - 1] == 1)?0:1);
            }
            byte[] decodedBytes = new byte[4];
            decodedBytes[0] = bytes[2];
            decodedBytes[1] = bytes[4];
            decodedBytes[2] = bytes[5];
            decodedBytes[3] = bytes[6];
            for (int i = 0; i < 4; i++) {
                System.out.print("" + ((decodedBytes[i] == 1)?1:0));
            }
            System.out.println("");
        }
    }
}

https://algospot.com/judge/submission/detail/213654 c로 짰는데 오답으로 계속 나와서 java로 컨버젼했더니 정답처리 되네요 도대체 뭐가 문제인지 ㅠㅠ

syshin commented 10 years ago

https://algospot.com/judge/submission/detail/214010

import java.util.HashMap;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int cases = sc.nextInt();
        while(cases-- > 0) {
            String inHammingCode = sc.next();
            //System.out.println("["+inHammingCode+"]");

            inHammingCode = inHammingCode.trim();           
            int[] tx = {0,0,0,0,0,0,0};
            for(int i=0; i<inHammingCode.length(); i++){                
                tx[i] = inHammingCode.charAt(i)-48;
            }

            /*
            for(int i=0; i<tx.length; i++){
                System.out.println("i : " + i + ", transmitted : " + tx[i]);
            }
            */
            int s1 = tx[0] ^ tx[2] ^ tx[4] ^ tx[6];
            int s2 = tx[1] ^ tx[2] ^ tx[5] ^ tx[6];
            int s3 = tx[3] ^ tx[4] ^ tx[5] ^ tx[6];

            //System.out.println(s1 + ":" + s2 + ":" + s3);

            int indicator = s3*4 + s2*2 + s1;
            //System.out.println(indicator);

            if(indicator != 0){
                tx[indicator-1] = tx[indicator-1] ^ 1;
            }

            /*
            for(int i=0; i<tx.length; i++){
                System.out.println("i : " + i + ", transmitted : " + tx[i]);
            }
            */

            System.out.print(tx[2]);
            System.out.print(tx[4]);
            System.out.print(tx[5]);
            System.out.print(tx[6]);

            System.out.println();

        }
    }
}