24h購物| | PChome| 登入
2013-06-12 08:40:47| 人氣897| 回應0 | 上一篇 | 下一篇

[UVA] 598 - Bundling Newspapers

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


  Bundling Newspapers 

A statistical research company needs a program to produce instructions for an existing application that creates questionnaires about newspapers. The application requires instructions that are comma- delimited lists of newspapers, each list consisting of a subset of the set of newspapers in some metropolitan area. Your program will read a list of newspapers and produce this output in a particular order, and for subsets of some range of sizes.

Input 

The first line of the input is an integer M, then a blank line followed by M datasets. There is a blank line between datasets.

Each dataset consists of a first line describing the sizes of subsets to be output, followed by the names of the newspapers, one per line. Each line is terminated by an end-of-line marker. The newspaper names will contain at most 30 characters each, and there will be a maximum of 12 newspaper names.


The first line will contain either a single asterisk in column one, one positive integer, or two positive integers, the first less than the second. Any integers in this line will be no greater than the number of newspapers to follow. The meaning of these is as follows:


* 		 means show all subset sizes from 1 to the number of newspapers in the list 
$a  b$
means show all subset sizes from a to b, inclusive 
n means show only subsets of size n

Output 

The output for each dataset should consist of the comma-delimited subsets of papers, one subset per line, grouped by subset size, and with a header line above each subset size group indicating the size. Print a blank line after each subset size group. Within a subset size group, the order of the lines of output should be listed in such a way that if the names of the first, second and third newspapers listed in the input file were A, B, C, etc., respectively, the output lines would be in lexicographic order (in the example above, A, B; A, C; A, D; B, C; ...).

Print a blank line between datasets.

Sample Input 

1

2 3
Times
Herald-Tribune
Post
New Advocate

Sample Output 

Size 2
Times, Herald-Tribune
Times, Post
Times, New Advocate
Herald-Tribune, Post
Herald-Tribune, New Advocate
Post, New Advocate

Size 3
Times, Herald-Tribune, Post
Times, Herald-Tribune, New Advocate
Times, Post, New Advocate
Herald-Tribune, Post, New Advocate



Miguel A. Revilla
1999-01-11

覺得還是有必須讓大家認識一下 sscanf 好用的地方。


#include <stdio.h>
#include <iostream>
#include <sstream>
using namespace std;
char word[20][50], line[50];
int n, path[20];
void dfs(int idx, int m, int i) {
    if(idx == m) {
        for(i = 0; i < idx; i++) {
            if(i)   printf(", ");
            printf("%s", word[path[i]]);
        }
        puts("");
        return;
    }
    for(; i < n; i++) {
        path[idx] = i;
        dfs(idx+1, m, i+1);
    }
}
int main() {
    int testcase;
    int a, b, i;
    scanf("%d", &testcase);
    while(getchar() != '\n');
    while(getchar() != '\n');
    while(testcase--) {
        gets(line);
        n = 0;
        while(gets(word[n])) {
            if(word[n][0] == '\0')   break;
            n++;
        }
        if(line[0] == '*') {
            a = 1, b = n;
        } else {
            if(sscanf(line, "%d %d", &a, &b) == 2) {}
            else    sscanf(line, "%d", &a), b = a;
        }
        for(i = a; i <= b; i++) {
            printf("Size %d\n", i);
            dfs(0, i, 0);
            puts("");
        }
        if(testcase)    puts("");
    }
    return 0;
}
/*
3

2 3
Times
Herald-Tribune
Post
New Advocate

1
Times
Herald-Tribune

*
Chinese Times
Union Times
Free Post
*/

台長: Morris
人氣(897) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][math] 10460 - Find the Permuted String
此分類上一篇:[UVA][greedy] 1193 - Radar Installation

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