2014年3月2日 星期日

NTHU OJ - 1003 OTAKU

The following program is my ACcepted code for NTHU-7488.
It's a for everybody to learn and discuss.
If there is any mistake or comment, please let me know.  :D

此乃NTHU 7488 的AC code!
歡迎一同討論學習,如有錯誤與任何建議請留言 : )

點這裡看題目 Click here to see this Problem!

我認為也可使用暴力單純一個個搜尋 (時間限制是10秒)
但是我是使用map的簡單應用AC
我之前習慣使用 (m[tmpStr] != 0) 來代表之前沒有存入 map 中的 key 值
但可能因為 map 之前沒有 access 過這個key值,所以 NTHU OJ把這種狀況視為 Runtime Error(SIGABRT).
改成 if(m.count(tmpStr) > 0) 即可AC

I think using brute force to search the key is still fine. (time limit: 10 s)
But I use simple STL map to solve it.
I used to apply (m[tmpStr] != 0) to represent the key, which was not saved in map.
But maybe map didn't access this key value so NTHU OJ think that's Runtime Error(SIGABRT).
I modifed it to if(m.count(tmpStr) > 0) and got AC.



//This program is for NTHU 1003 OTAKU
//http://acm.cs.nthu.edu.tw/problem.php?pid=1003

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<map>
using namespace std;

int main()
{
    int N, M, K;
    map<string, int> m;
    string tmpStr;
   
    while(scanf("%d %d", &N, &M) != EOF)
    {
        m.clear();
       
        for(int i=0; i<N; i++)
        {
            cin >> tmpStr;
            //cout << tmpStr << endl;//check
            m[tmpStr] = 1;
        }
       
        for(int i=0; i<M; i++)
        {
            scanf("%d", &K);
           
            int feature = 0;
           
            for(int j = 0; j<K; j++)
            {
                cin >> tmpStr;
               
                // I used (m[tmpStr] != 0) before, but it will get "Runtime Error(SIGABRT)"!!!
                if(m.count(tmpStr) > 0)
                    feature ++;
            }
           
            if(feature*2 > K)
                printf("Y\n");
            else
                printf("N\n");
        }
    }
   
    return 0;
}


Please feel free to use it after adding this blog as an reference. (http://autekroy.blogspot.tw) If there is any mistake or comment, please let me know. :D 

歡迎使用與分享任何內容,但請記得標示此部落格為出處。(http://autekroy.blogspot.tw/) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )

沒有留言:

張貼留言

請留下您的任何想法或建議!
Please leave any thought or comment!