POJ 2136 Vertical Histogram(我的水题之路——字符统计表)

来源:互联网 发布:编写汇编语言的软件 编辑:程序博客网 时间:2024/05/20 07:14
Vertical Histogram
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 13895 Accepted: 6697

Description

Write a program to read four lines of upper case (i.e., all CAPITAL LETTERS) text input (no more than 72 characters per line) from the input file and print a vertical histogram that shows how many times each letter (but not blanks, digits, or punctuation) appears in the all-upper-case input. Format your output exactly as shown.

Input

* Lines 1..4: Four lines of upper case text, no more than 72 characters per line.

Output

* Lines 1..??: Several lines with asterisks and spaces followed by one line with the upper-case alphabet separated by spaces. Do not print unneeded blanks at the end of any line. Do not print any leading blank lines. 

Sample Input

THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG.THIS IS AN EXAMPLE TO TEST FOR YOURHISTOGRAM PROGRAM.HELLO!

Sample Output

                            *                            *        *                   *        *                   *     *   *        *                   *     *   **       *     *             *     *   **       *     * *     * *   *     * * **       *   * * *     * *   * *   * * * **     * * * * * *     * * * * *   * * * *     * ** * * * * * * * * * * * * * * * * * * * * * * * * *A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Source

USACO 2003 February Orange

统计Input中前四行的字符串中各个大写字母的个数,然后用如上图表表示出来。

这道题之前做过一次,当时用的是一个二维数组模拟的方式,这一次重新做,用的是一个两个一维数组分别统计各个字母个数以及各行末尾结束的位置。先输入的时候统计出各个字母的个数,存在arr数组中并且统计到其中的最大值max。然后对于数组arr从下往上遍历,找到末尾的位置存在end数组中。末尾的位置只有max个,也就是说,从右边开始每一行只有一个。方法是,从'Z'到'A'的个数,如果比当时已经统计到的位置top大,就将end数组的top到当前arr[i]的下标元素赋值为i,即可统计出末尾。直到统计到max即可停止循环。

注意点:
1)各个字母之间有一个空格分隔,‘Z’后面没有。
2)每一行的最后一个‘*’之后没有任何空格,可以用任何元素代替‘ ’来进行调试。

代码(1AC):
#include <cstdio>#include <cstdlib>#include <cstring>int arr[30];int max;int end[400];int top;int main(void){    int i, j;    char ch;    memset(arr, 0, sizeof(arr));    max = 0;    for (i = 0; i < 4; i++){        while ((ch = getchar()) != '\n'){            if (ch >= 'A' && ch <= 'Z'){                arr[ch - 'A']++;                max = max > arr[ch - 'A'] ? max : arr[ch - 'A'];            }        }    }    for (i = 26, top = 0; max >= top && i >= 0; i--){        if (arr[i] > top){            while (top < arr[i]){                end[++top] = i;            }            end[top] = i;        }    }    for (i = max; i >= 1; i--){        for (int j = 0; j <= end[i]; j++){            if (arr[j] >= i){                putchar('*');            }            else {                putchar(' ');            }            if (j != end[i]){                putchar(' ');            }            else{                putchar('\n');            }        }    }    for (i = 0; i < 25; i++){        printf("%c ", 'A' + i);    }    printf("Z\n");    return 0;}


原创粉丝点击