Csu1565:word Cloud

Source: Internet
Author: User

Description

A word cloud (or tag cloud) is a visual representation of textual data based on a weighted metric. In the above cloud (which was based on this year's list of mid-central teams), the font size of each word was based on its n Umber of occurrences in the data set. Tagg Johnson is a mans obsessed with counting words that appear in online documents. On him computer, he keeps a spreadsheet of all the sites he visits, along with a list of words that appear on each site an d the number of times such word appears. Tagg would like to generate word clouds based on the data he has collected.
Before describing the algorithm Tagg uses for generating clouds, we digress for a quick lesson in typography. The basic unit of measure is known as a point (typically abbreviated as PT). A font ' s size is described based on the vertical number of points from one line to the next, including any interline Spaci Ng. For example, with a 12pt font, the vertical space from the top of one character to the top of a character below it is P Oints. We assume that a character's height is precisely equal to the font's point size (regardless of whether the character are up per or lower case).

for This problem, we focus on a fixed-width font, such as Courier, and which each character of the Alphab ET is also given the same amount of width. The character width for such a font depends on the font size and the aspect ratio. For Courier, a word with T characters rendered in a font of size P have a total width of  when measured in points . Note well the use of the ceiling operator, which converts all noninteger to the next highest integer. For example, a 5-letter word in a 20pt font would is rendered with a height of points and a width equal to points.

now We can describe Tagg ' s algorithm for creating a word cloud. He pre-sorts His word list to alphabetical order and removes words that does not occur at least five times. For each word w, he computes a point size based on the formula , where CW is the number of occurrences of the word, a nd Cmax is the number of occurrences of the frequent word in the data set. Note that by this formula, every word would be rendered with anywhere from a 9pt font to a 48pt font. He then places the words in rows, with a 10pt horizontal space between adjacent words, placing as many words as fit in the Row, subject to a maximum width W for his entire cloud. The height of a given row is equal to the maximum font size of any word rendered in that row.

As a tangible example, consider the following data set and Word cloud.

Word Count

Apple 10
Banana 5
Grape 20
Kiwi 18
Orange 12
Strawberry 10

In this example, Apple are rendered with 23pt font using width 65pt, banana are rendered with 11pt font using width 38pt, an D grape is rendered with 48pt font and width 135pt. If The overall word cloud is constrained to has the width at most 260, those three words fit in a row and the overall height Of that row was 48pt (due to grape). On the second row Kiwi are rendered with height 43pt and width 97pt, and orange are rendered with height 28pt and width 95pt . A third row has strawberry with height 23pt and width 130pt. The overall height of this word cloud is 114pt.

Input

Each data set begins with a line containing the integers:w and N. The value W denotes the maximum width of the cloud; w≤5000 'll is at least as wide as any word at its desired font size. The value 1≤n≤100 denotes the number of words that appear in the cloud. Following the first line was N additional lines, each had a string S that's the word (with no whitespace), and an inte Ger C That's a count of the number of occurrences of that word in the original data set, with 5≤c≤1000. Words'll is given in the same order that they is to be displayed within the cloud.

Output

For each data set, output of the word CLOUD followed by a space, a serial number indicating the data set, a colon, another SP Ace, and the integer height of the cloud, measured in font points.

Sample Input
260 6apple 10banana 5grape 20kiwi 18orange 12strawberry 10250 6apple 10banana 5grape 20kiwi 18orange 12strawberry 10610 6a Pple 10banana 5grape 20kiwi 18orange 12strawberry 100 0
Sample Output
CLOUD 1:114cloud 2:99cloud 3:48
HINT

Source
Test instructions: Although the topic is very long, but in fact is a water problem, there are several words, according to test instructions can find the width and height of each word, there is a width fixed paper, in order to print the word, but the same line of words between each other to separate 10 units, and the second word place must be above the maximum height of the row, So it's a simple simulation situation.
#include <iostream> #include <stdio.h> #include <string.h> #include <stack> #include <queue > #include <map> #include <set> #include <vector> #include <math.h> #include <algorithm >using namespace std, #define LS 2*i#define rs 2*i+1#define up (i,x,y) for (i=x;i<=y;i++) #define DOWN (i,x,y) for (i=x; i>=y;i--) #define MEM (a,x) memset (A,x,sizeof (a)) #define W (a) while (a) #define LL long longconst double pi = acos (-1.0); # Define Len 100005 int W,n;char str[105][1005];int S[105],P[105],W[105],LEN[105],MAXN;    int main () {int i,j,k,cas = 1,ans;        W (~scanf ("%d%d", &w,&n)) {if (w+n==0) break;        MAXN = 0;            Up (i,0,n-1) {scanf ("%s%d", Str[i],&s[i]);            Len[i] = strlen (Str[i]);        MAXN = max (maxn,s[i]);            } up (i,0,n-1) {P[i] = 8+ (int) ceil (1.0*40* (s[i]-4)/(maxn-4));        W[i] = (int) ceil (1.0*9*LEN[I]*P[I]/16);      } ans = 0;  int flag = 1,maxh=0,now;                Up (i,0,n-1) {if (flag) {ans+=maxh;                maxh=0;                flag=0;                Maxh=max (Maxh,p[i]);            Now=w[i];                    } else {if (now+10+w[i]<=w) {now+=10+w[i];                Maxh=max (Maxh,p[i]);                    } else {flag=1;                i--;    }}} printf ("CLOUD%d:%d\n", cas++,ans+maxh); } return 0;}


Csu1565:word Cloud

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.