熱門文章

2025年12月18日 星期四

ZeroJudge 解題筆記:a131. 00739 - Soundex Indexing

作者:王一哲
日期:2025年12月18日


ZeroJudge 題目連結:a131. 00739 - Soundex Indexing

解題想法


可以用陣列建立字母、數字對照表,將字母 A, B, C, ..., Z 對應的數字填入陣列之中。也可以用字典建立對照表,這個寫法比較容易閱讀。

Python 程式碼


使用時間約為 18 ms,記憶體約為 3.3 MB,通過測試。
import sys

table = ('0', '1', '2', '3', '0', '1', '2', '0', '0', '2',
         '2', '4', '5', '5', '0', '1', '2', '6', '2', '3',
         '0', '1', '0', '2', '0', '2')
print("         NAME                     SOUNDEX CODE")
for line in sys.stdin:
    s = line.strip()
    n = len(s)
    last = table[ord(s[0]) - ord('A')]
    result = [s[0]]
    for i in range(1, n):
        if len(result) >= 4: break
        curr = table[ord(s[i]) - ord('A')]
        if curr != '0' and curr != last: result.append(curr)
        last = curr
    while len(result) < 4: result.append('0')
    output = " "*9 + s + " "*(25-n) + "".join(result)
    print(output)
print("                   END OF OUTPUT")


使用時間約為 18 ms,記憶體約為 3.3 MB,通過測試。
import sys

table = {'B': '1', 'P': '1', 'F': '1', 'V': '1',
         'C': '2', 'S': '2', 'K': '2', 'G': '2', 'J': '2', 'Q': '2', 'X': '2', 'Z': '2',
         'D': '3', 'T': '3',
         'L': '4',
         'M': '5', 'N': '5',
         'R': '6'}
print("         NAME                     SOUNDEX CODE")
for line in sys.stdin:
    s = line.strip()
    n = len(s)
    last = table.get(s[0], '0')
    result = [s[0]]
    for i in range(1, n):
        if len(result) >= 4: break
        curr = table.get(s[i], '0')
        if curr != '0' and curr != last: result.append(curr)
        last = curr
    while len(result) < 4: result.append('0')
    output = " "*9 + s + " "*(25-n) + "".join(result)
    print(output)
print("                   END OF OUTPUT")


C++ 程式碼


使用時間約為 2 ms,記憶體約為 324 MB,通過測試。
#include <iostream>
#include <string>
using namespace std;

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    char table[26] = {'0', '1', '2', '3', '0', '1', '2', '0', '0', '2',
                      '2', '4', '5', '5', '0', '1', '2', '6', '2', '3',
                      '0', '1', '0', '2', '0', '2'};
    cout << "         NAME                     SOUNDEX CODE\n";
    string s;
    while(cin >> s) {
        int n = (int)s.size();
        char last = table[s[0] - 'A'];
        string result (1, s[0]);
        for(int i=1; i<n; i++) {
            if (result.size() >= 4) break;
            char curr = table[s[i] - 'A'];
            if (curr != '0' && curr != last) result += curr;
            last = curr;
        }
        while(result.size() < 4) result += '0';
        string output (9, ' ');
        output += s;
        for(int i=0; i<25-n; i++) output += " ";
        output += result;
        cout << output << "\n";
    }
    cout << "                   END OF OUTPUT\n";
    return 0;
}

使用時間約為 2 ms,記憶體約為 336 MB,通過測試。
#include <iostream>
#include <string>
#include <map>
using namespace std;

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    map<char, char> table = 
        {{'B', '1'}, {'P', '1'}, {'F', '1'}, {'V', '1'},
         {'C', '2'}, {'S', '2'}, {'K', '2'}, {'G', '2'}, {'J', '2'}, {'Q', '2'}, {'X', '2'}, {'Z', '2'},
         {'D', '3'}, {'T', '3'},
         {'L', '4'},
         {'M', '5'}, {'N', '5'},
         {'R', '6'},
         {'A', '0'}, {'E', '0'}, {'I', '0'}, {'O', '0'}, {'U', '0'}, {'Y', '0'}, {'W', '0'}, {'H', '0'}};
    cout << "         NAME                     SOUNDEX CODE\n";
    string s;
    while(cin >> s) {
        int n = (int)s.size();
        char last = table[s[0]];
        string result (1, s[0]);
        for(int i=1; i<n; i++) {
            if (result.size() >= 4) break;
            char curr = table[s[i]];
            if (curr != '0' && curr != last) result += curr;
            last = curr;
        }
        while(result.size() < 4) result += '0';
        string output (9, ' ');
        output += s;
        for(int i=0; i<25-n; i++) output += " ";
        output += result;
        cout << output << "\n";
    }
    cout << "                   END OF OUTPUT\n";
    return 0;
}


沒有留言:

張貼留言