menu SmallWang
more_vert
chevron_right 首页 » 杂项 » 正文
accept
2020-03-30 | 杂项 | 暂无评论 | 73 次阅读 | 0字
import java.io.IOException;
import java.io.InputStreamReader;

public class hdu1880jiangpan 
{

    private static final int MaxSize = 100000;

    private static final CompressString keys = new CompressString(MaxSize * 21);

    private static final CompressString descriptions = new CompressString(
            MaxSize * 81);

    private static final char[] search = new char[80];

    private static int searchLength;

    private static int readQCount(InputStreamReader cin) throws IOException 
    {
        int letter = 0;
        int qCount = 0;
        while (true) 
        {
            letter = cin.read();
            if (letter != '\r' && letter != '\n') 
            {
                break;
            }
        }
        qCount = letter - '0';
        while (true) 
        {
            letter = cin.read();
            if (letter == '\r' || letter == '\n') 
            {
                break;
            }
            qCount = qCount * 10 + letter - '0';
        }
        return qCount;
    }

    private static void readLine(InputStreamReader cin) throws IOException 
    {
        searchLength = 0;
        int letter = 0;
        while (true) 
        {
            letter = cin.read();
            if (letter != '\r' && letter != '\n') 
            {
                break;
            }
        }
        search[searchLength++] = (char) letter;
        while (true) 
        {
            letter = cin.read();
            if (letter == '\r' || letter == '\n') 
            {
                break;
            }
            search[searchLength++] = (char) letter;
        }

    }

    public static void main(String args[]) throws Exception 
    {
        InputStreamReader cin = new InputStreamReader(System.in);
        input(cin);
        query(cin);
    }

    private static void input(InputStreamReader cin) throws IOException 
    {
        boolean iskey = false;
        int length = 0;
        while (true) 
        {
            char letter = (char) cin.read();
            if (letter == '\r' || letter == '\n') 
            {
                if (length != 0) 
                {
                    descriptions.finish(length);
                    length = 0;
                }
            } else if (letter == '[') 
            {
                iskey = true;
            } else if (letter == ']') 
            {
                keys.finish(length);
                length = 0;
                iskey = false;
                cin.read();
            } else if (letter == '@' && length == 0)
            {
                cin.read();
                cin.read();
                cin.read();
                cin.read();
                break;
            } else 
            {
                if (iskey) 
                {
                    keys.put(letter, length);
                } else {
                    descriptions.put(letter, length);
                }
                length++;
            }
        }
    }

    private static void query(InputStreamReader cin) throws IOException 
    {
        int qCount = readQCount(cin);
        for (int i = 0; i < qCount; i++) 
        {
            readLine(cin);
            if (search[0] == '[') 
            {
                System.arraycopy(search, 1, search, 0, searchLength - 2);
                searchLength -= 2;
                int ans = keys.find();
                if (ans == -1) 
                {
                    System.out.println("what?");
                } else {
                    descriptions.dispaly(ans);
                }
            } else 
            {
                int ans = descriptions.find();
                if (ans == -1) 
                {
                    System.out.println("what?");
                } else 
                {
                    keys.dispaly(ans);
                }
            }
        }
    }

    private static class CompressString 
    {
        private char[] letters;

        private int letterCount;

        public CompressString(int capacity) 
        {
            letters = new char[capacity];
        }

        public void put(char letter, int index) 
        {
            letters[letterCount + 1 + index] = letter;
        }

        public void finish(int length) 
        {
            letters[letterCount] = (char) length;
            letterCount += length + 1;
        }

        public int find() 
        {
            int pos = 0;
            int counter = 0;
            while (pos < letterCount) 
            {
                int length = letters[pos];
                if (length == searchLength) 
                {
                    if (equals(pos + 1, letters)) 
                    {
                        return counter;
                    }
                }
                pos += length + 1;
                counter++;
            }
            return -1;
        }

        private void dispaly(int skipCounter) 
        {
            int pos = 0;
            for (int i = 0; i < skipCounter; i++) 
            {
                pos += letters[pos] + 1;
            }
            int length = letters[pos++];
            for (int i = 0; i < length; i++) 
            {
                System.out.print(letters[i + pos]);
            }
            System.out.println();
        }

        private static boolean equals(int start, char[] words) 
        {
            for (int i = 0; i < searchLength; i++) 
            {
                if (words[start + i] != search[i]) 
                {
                    return false;
                }
            }
            return true;
        }
    }
}
None
发表评论
暂无评论
textsms
account_circle
email
link
arrow_back 上一篇
arrow_forward 下一篇
没有了