menu SmallWang
more_vert
chevron_right 首页 » ACM » 正文
Attempted
2020-03-28 | ACM | 暂无评论 | 77 次阅读 | 0字
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.StringTokenizer;

public class Main
{
    public static InputReader in = new InputReader(System.in);
    public static PrintWriter out = new PrintWriter(new OutputStreamWriter(
            System.out));

    public static void main(String[] args) throws IOException
    {
        hashTable m = new hashTable();
        hashTable mm = new hashTable();
        while (in.hasNext())
        {
            String s = in.nextLine();
            if (s.substring(0, 1).equals("["))
            {
                int i = s.indexOf("]");
                // System.out.println(s.substring(0, i+1)+"     /"+
                // s.substring(i + 2));
                // System.out.println(s.substring(0, i + 1));
                m.insert(s.substring(0, i + 1), s.substring(i + 2));
                mm.insert(s.substring(i + 2), s.substring(0, i + 1));
            } else
                break;
        }
        // System.out.println(s);
        String s = in.nextLine();
        for (int i = 0; i < Integer.valueOf(s); i++)
        {
            String ss = in.nextLine();
            if (ss.indexOf("]") != -1)
            {
                out.println(m.find(ss));
            } else
            {
                String sss = mm.find(ss);
                if (sss.equals("what?"))
                    out.println("what?");
                else
                    out.println(sss.substring(1, sss.length() - 1));
            }
        }
        out.flush();
    }
}

class hashTable
{
    static final int a = 9999;
    hashNode[] table = new hashNode[10000];

    void insert(String s1, String s2)
    {
        hashNode x = new hashNode(s1, s2);
        // System.out.println(x.hash);
        // System.out.println(x.s1 + "---" + x.hash);
        if (table[x.hash] == null)
            table[x.hash] = x;
        // System.err.println(table[x.hash].s1);
        else
        {
            x.next = table[x.hash].next;
            table[x.hash].next = x;
        }
    }

    String find(String s1)
    {
        int hash = 1;
        int count = 0;
        for (char c : s1.toCharArray())
        {
            if (count++ < 8)
            {
                hash *= c;
                hash %= 9999;
            }
        }
        // System.out.println(s1 + "----" + hash);
        if (table[hash] == null)
            return "what?";
        // System.out.println("----" + table[hash].s1);
        if (table[hash].hash == hash && table[hash].s1.equals(s1))
            return table[hash].s2;
        else
        {
            hashNode pos = table[hash % a];
            while (pos.next != null)
            {
                if (pos.hash == hash && pos.s1.equals(s1))
                    return pos.s2;
                pos = pos.next;
            }
            return "what?";
        }
    }
}

class hashNode
{
    int hash;
    String s1;
    String s2;
    hashNode next = null;

    public hashNode(String s1, String s2)
    {
        this.s1 = s1;
        this.s2 = s2;
        int count = 0;
        hash = 1;
        for (char c : s1.toCharArray())
        {
            if (count++ < 8)
            {
                hash *= c;
                hash %= 9999;
            }
        }
        // System.out.println(s1 + "----" + hash);
    }

}

class InputReader
{
    private BufferedReader in;
    private StringTokenizer Tokenizer;

    public InputReader(InputStream stream)
    {
        in = new BufferedReader(new InputStreamReader(stream), 32768);
        Tokenizer = null;
    }

    public String next() throws IOException
    {
        while (Tokenizer == null || !Tokenizer.hasMoreTokens())
        {
            Tokenizer = new StringTokenizer(in.readLine());
        }
        return Tokenizer.nextToken();
    }

    public int nextInt() throws IOException
    {
        return Integer.valueOf(next());
    }

    public String nextLine() throws IOException
    {
        while (Tokenizer == null || !Tokenizer.hasMoreTokens())
        {
            Tokenizer = new StringTokenizer(in.readLine(), "\n");
            if (!Tokenizer.hasMoreTokens())
                return "\n";
        }
        return Tokenizer.nextToken("\n");
    }

    public BigInteger nextBigInteger() throws IOException
    {
        return new BigInteger(next());
    }

    public BigDecimal nextBigDecimal() throws IOException
    {
        return new BigDecimal(next());
    }

    public double nextDouble() throws IOException
    {
        return Double.valueOf(next());
    }

    boolean hasNext()
    {
        while (Tokenizer == null || !Tokenizer.hasMoreTokens())
        {
            try
            {
                Tokenizer = new StringTokenizer(in.readLine());
            } catch (Exception e)
            {
                return false;
            }
        }
        return true;
    }
}
None
发表评论
暂无评论
textsms
account_circle
email
link
arrow_back 上一篇
arrow_forward 下一篇