24h購物| | PChome| 登入
2012-07-11 21:58:13| 人氣378| 回應0 | 上一篇 | 下一篇

[UVA] 789 - Indexing

推薦 0 收藏 0 轉貼0 訂閱站台

Input :
T
CONSIDER SEVERAL LINES OF TEXT AND ONE CHARCTER CALLED KEY,
SPECIFIED ON A LINE PRECEDING THE TEXT. THE WORDS IN THE TEXT ARE
SEPARAED BY SINGLE SPACE. THERE ARE NO WORDS SPLITTED BETWEEN TWO
LINES.
WRITE A PROGRAM TO DISPLAY AN INDEX OF ALL WORDS WIHIN THE TEXT
STARTING WITH THE KEY. THE INDEX MUST BE ALPHABETICALLY SORTED. EACH
WORD IN THE INDEX MUST BE FOLLOWED BY THE LIST OF THE LINE NUMBERS IN
WHICH IT APPEARS.


討論區說這題沒有 測試資料, 也就是說不用輸出也會過 ?
不過我一次 AC 了, 沒多想什麼


#include <cstdio>
#include <iostream>
#include <map>
#include <set>
using namespace std;

int main() {
    char key = getchar();
    cin.ignore(1024, '\n');
    string s;
    int idx = 0;
    map<string, set<int> > r;
    while(getline(cin, s)) {
        ++idx;
        int i, len = s.length();
        for(i = 0; i < len; i++) {
            if(!isalpha(s[i]))
                continue;
            int j(i);
            string t = "";
            while(j < len) {
                if(isalpha(s[j]))
                    t += s[j];
                else
                    break;
                j++;
            }
            i = j;
            if(t[0] == key) {
                r[t].insert(idx);
            }
        }
    }
    for(map<string, set<int> >::iterator i = r.begin(); i != r.end(); i++) {
        cout << i->first;
        for(set<int>::iterator j = i->second.begin(); j != i->second.end(); j++)
            cout << " " << *j;
        cout << endl;
    }
    return 0;
}

台長: Morris
人氣(378) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 892 - Finding words
此分類上一篇:[UVA] 153 - Permalex

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文