首页 > 试题广场 >

Paragraph

[问答题]

Paragraph

问题描述给定一个段落,由 N 个句子组成。第 i 个句子的长度为 L[i],包含的单词个数为 W[i]

句子不包含任何除字母和空格( ) 外的符号。

每个句子内部,含有若干个单词,由空格( ) 分隔。句子不会包含连续的空格。

随后给定 M 个查询,每个查询包含一个句子,需要在段落中寻找相同单词数量最多的句子。重复的单词只计一次,且不区分大小写。

输入数据将保证结果是存在且唯一的。

输入格式

第一行是两个整数 NM

接下来的 N+M 行,每行包含一个句子。

N 行代表段落中的句子,后 M 行表示查询。

输出格式

输出 M 行,每行代表查询的结果。

输入样例

6 3

An algorithm is an effective method that can be expressed within a finite amount of space and time

Starting from an initial state and initial input the instructions describe a computation

That when executed proceeds through a finite number of successive states

Eventually producing output and terminating at a final ending state

The transition from one state to the next is not necessarily deterministic

Some algorithms known as randomized algorithms incorporate random input

Next to the transition

Wormhole, infinite time and space

The transition from one state to the next is not necessarily deterministic

输出样例

The transition from one state to the next is not necessarily deterministic

An algorithm is an effective method that can be expressed within a finite amount of space and time

The transition from one state to the next is not necessarily deterministic

数据规模

0 < L[i] < 512

0 < W[i] < 32

对于 30% 的数据,0 < N < 300 < M < 30

对于 100% 的数据,0 < N < 5000 < M < 800

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int M = sc.nextInt();
        sc.nextLine();
        String[] sentences = new String[N];
        for (int i = 0; i < N; i++) {
            sentences[i] = sc.nextLine();
        }
        List<HashSet<String>> sentenceList = new ArrayList<>(N);
        for (int i = 0; i < N; i++) {
            HashSet<String> set = new HashSet<>();
            String[] temp = sentences[i].toLowerCase().split(" ");
            for (String s : temp) {
                set.add(s);
            }
            sentenceList.add(set);
        }

        for (int i = 0; i < M; i++) {
            HashSet<String> set = new HashSet<>();
            String[] temp = sc.nextLine().toLowerCase().split(" ");
            for (String s : temp) {
                set.add(s);
            }

            int max = 0;
            int maxIndex = 0;
            for (int j = 0; j < N; j++) {
                HashSet<String> tarSentence = sentenceList.get(j);
                int count = 0;
                for (String s : set) {
                    if (tarSentence.contains(s)) {
                        count++;
                    }
                }
                if (count > max) {
                    max = count;
                    maxIndex = j;
                }
            }
            System.out.println(sentences[maxIndex]);
        }
    }
}
the output is that:
6 3
An algorithm is an effective method that can be expressed within a finite amount of space and time
Starting from an initial state and initial input the instructions describe a computation
That when executed proceeds through a finite number of successive states
Eventually producing output and terminating at a final ending state
The transition from one state to the next is not necessarily deterministic
Some algorithms known as randomized algorithms incorporate random input
Next to the transition
The transition from one state to the next is not necessarily deterministic
Wormhole, infinite time and space
An algorithm is an effective method that can be expressed within a finite amount of space and time
The transition from one state to the next is not necessarily deterministic
The transition from one state to the next is not necessarily deterministic
编辑于 2017-08-22 16:18:16 回复(0)
#include <bits/stdc++.h> using namespace std;
int main() {
    int n, m;
    char cc;
    cin >> n >> m;
    vector<unordered_set<string>> setence(0);
vector<string> vec(0);     string str;
    getline(cin, str);
    for(int k = 0; k < n; k++) {
        getline(cin, str);
        vec.push_back(str);
        unordered_set<string> tmp;
        tmp.clear();
        string word = "";
        for(int i = 0; i < str.size(); i++) {
            if(str[i] == ' ') {
                if(word.size() > 0) {
                    transform(word.begin(), word.end(), word.begin(), ::tolower);
                    tmp.insert(word);
                }
                word = "";
            } else if(str[i] != ' ') {
                word += str[i];
            }
        }
        if(word.size() > 0) {
            transform(word.begin(), word.end(), word.begin(), ::tolower);
            tmp.insert(word);
        }
        setence.push_back(tmp);
    }
    for(int k = 0; k < m; k++) {
        getline(cin, str);
        unordered_set<string> tmp;
        tmp.clear();
        
        string word = "";
        for(int i = 0; i < str.size(); i++) {
            if(str[i] == ' ') {
                if(word.size() > 0) {
                    transform(word.begin(), word.end(), word.begin(), ::tolower);
                    tmp.insert(word);
                }
                word = "";
            } else if(str[i] != ' ') {
                word += str[i];
            }
        }
        if(word.size() > 0) {
            transform(word.begin(), word.end(), word.begin(), ::tolower);
            tmp.insert(word);
        }
        word = "";
        
        int index = 0, mx = 0;
        for(int i = 0; i < n; i++) {
            int count = 0;
            for(auto it = tmp.begin(); it != tmp.end(); it++) {
                if(setence[i].find(*it) != setence[i].end()) {
                    ++count;
                }
            }
            if(count > mx) {
                mx = count;
                index = i;
            }
        }
        cout << vec[index] << endl;
    }
    return 0;
}
发表于 2017-07-28 16:45:03 回复(0)
NM=raw_input().strip().split()
N,M=map(int,NM)
list=[]
for i in range(N+M):
    list.append(raw_input())

list1 = []
for i in list:
    list1.append(i.strip().split())

count=0
list3=[]
for i in range(N, N + M):
    list2 = []
    for k in range(N):
        for j in list1[i]:
            if j in list1[k]:
                count+=1
        list2.append(count)
        count=0
    list3.append(list2)

for i in range(len(list3)):
    indexi=list3[i].index(max(list3[i]))
    print list[indexi]

发表于 2017-08-05 16:57:56 回复(0)