UVa 1597-Searching the Web

來源:互聯網
上載者:User

標籤:

這個題比較長,如果按照題目要求一步一步做的話。也不是很難。但是如果想提高代碼精簡度和可讀性高效性確實不容易,想輕鬆AC還是用C++,C語言的輸入輸出。Java很容易逾時。普通演算法要10s左右。

Java代碼 :

import java.util.*;public class Main1597 {public static void main(String[] args) {Scanner scan = new Scanner(System.in);int t = scan.nextInt();String s = scan.nextLine();String[][] str = new String[105][1500]; int[] row = new int[t];Arrays.fill(row, 0);for(int i=0; i<t; i++) {while(true) {str[i][row[i]] = scan.nextLine();if(str[i][row[i]].equals("**********")) {break;}row[i] ++;}}int n = scan.nextInt();String ss = scan.nextLine();for(int j=0; j<n; j++) {String line = scan.nextLine();String[][] ans = new String[t][1000];int[] cnt = new int[1000];Arrays.fill(cnt, 0);if(line.contains(" AND ")) {int end = 0;while(line.charAt(end)!=' ')end ++;String A = line.substring(0, end);String B = line.substring(end+5, line.length());for(int i=0; i<t; i++) {int a = 0, b = 0;for(int r=0; r<row[i]; r++) {if(str[i][r].toLowerCase().contains(A)) {a = 1;}if(str[i][r].toLowerCase().contains(B)) {b = 1;}}if(a==1 && b==1) {for(int r=0; r<row[i]; r++) {if(str[i][r].toLowerCase().contains(A) || str[i][r].toLowerCase().contains(B)) {ans[i][cnt[i]++] = str[i][r];}}}}}else if(line.contains(" OR ")) {int end = 0;while(line.charAt(end)!=' ')end ++;String A = line.substring(0, end);String B = line.substring(end+4, line.length());for(int i=0; i<t; i++) {for(int r=0; r<row[i]; r++) {if(str[i][r].toLowerCase().contains(A) || str[i][r].toLowerCase().contains(B)) {ans[i][cnt[i]++] = str[i][r];}}}}else if(line.contains("NOT ")) {int end = 0;while(line.charAt(end)!=' ')end ++;//String A = line.substring(0, end);String B = line.substring(4, line.length());for(int i=0; i<t; i++) {int con = 0;for(int r=0; r<row[i]; r++) {if(!str[i][r].toLowerCase().contains(B)) {//ans[i][cnt[i]++] = str[i][r];con ++;}}if(con == row[i]) {for(int r=0; r<row[i]; r++) {ans[i][cnt[i]++] = str[i][r];}}}}else {for(int i=0; i<t; i++) {for(int r=0; r<row[i]; r++) {if(str[i][r].toLowerCase().contains(line)) {ans[i][cnt[i]++] = str[i][r];}}}}int flag = 0, m = 0;for(int k=0; k<t; k++) {if(m > 0 && cnt[k] != 0)System.out.println("----------");for(int l=0; l<cnt[k]; l++) {//System.out.println("----------");    System.out.println(ans[k][l]);    flag = 1;    m ++;}}if(flag == 0)System.out.println("Sorry, I found nothing.");System.out.println("==========");}}}

來自互連網的C++代碼 :

//{標頭檔模板#include <algorithm>#include <bitset>#include <cassert>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <functional>#include <iomanip>#include <iostream>#include <list>#include <map>#include <queue>#include <set>#include <sstream>#include <stack>#include <string>#include <vector>using namespace std;//}標頭檔模板#define rep(i,b) for(int i=0; i<(b); i++)#define foreach(i,a) for(__typeof((a).begin()) i=a.begin(); i!=(a).end(); ++i)#define FOR for (int j = limit[i]; j < limit[i + 1]; j++)typedef bool Bit[1505];int n, lines, m;          // n文檔數, lines行數, m請求數int limit[105];           // limit[i]: 第i篇文檔從第幾行開始string doc[1505];         // 儲存內容map<string, Bit> Index;   // Index[單詞]: B標記了哪些行出現過// 用s來更新Indexvoid upDate(string s, int p) {    string word;    foreach(it, s) {        if (isalpha(*it)) *it = tolower(*it); // 變小寫        else *it = ' '; // 非字母變空白    }    stringstream ss(s);    while (ss >> word) Index[word][p] = true;}int main() {    //{section: 文檔資料輸入    scanf("%d ", &n);    rep(i, n) {        limit[i] = lines;        while (getline(cin, doc[lines]), doc[lines] != "**********") {            upDate(doc[lines], lines);            lines++;        }    }    limit[n] = lines;//}    //{section: 對擷取的請求輸出對應的內容    string  s;                      // s儲存每次得到的請求    Bit     mark;                   //{記錄哪些行應該輸出                                    //}mark的解釋: 因為每篇文檔要用10個'-'隔開,比較麻煩,最後統一處理    bool    *A, *B;    scanf("%d ", &m);    for (int iii = 0; iii < m; iii++) {        getline(cin, s);        //{subsection: 計算出mark中介        if (s[0] == 'N') {            A = Index[s.substr(4)];            rep(i, n) {                bool logo = true;                FOR if (A[j]) { logo = false; break; }                FOR mark[j] = logo;            }        } else if (s.find("AND") != string::npos) {            int p = s.find(" AND ");            A = Index[s.substr(0, p)];            B = Index[s.substr(p + 5)];            memset(mark, 0, sizeof(mark));            bool hasA, hasB;    // 在同一文章中, 兩個詞是否都出現            rep(i ,n) {                hasA = hasB = false;    // 預設沒出現                FOR if (A[j]) { hasA = true; break; }                FOR if (B[j]) { hasB = true; break; }                if (!(hasA&&hasB)) continue;                FOR mark[j] = (A[j] || B[j]);            }        } else if (s.find("OR") != string::npos) {            int p = s.find(" OR ");            A = Index[s.substr(0, p)];            B = Index[s.substr(p + 4)];            rep(i, lines) mark[i] = (A[i] || B[i]);        } else memcpy(mark, Index[s], sizeof(mark));//}        //{subsection: 輸出mark        bool hasOut = false, needOut = false;    // 記錄上一輪是否有輸出文檔        rep(i, n) {            if (hasOut) needOut = true;            hasOut = false;            FOR if (mark[j]) {                if (needOut) {                    cout << "----------\n";                    needOut = false;                }                cout << doc[j] << "\n";                hasOut = true;            }        }        if (!(needOut||hasOut)) cout << "Sorry, I found nothing.\n";        cout << "==========\n";//}    }//}    return 0;}



 

UVa 1597-Searching the Web

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.