POJ1420 Spreadsheet (topological sort) Note that hyper-memory

Source: Internet
Author: User
Tags integer numbers

Spreadsheet
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 617 Accepted: 290

Description

In 1979, Dan Bricklin and Bob Frankston wrote VisiCalc, the first spreadsheet application. It became a huge success and, at that time, is the killer application for the Apple II computers. Today, spreadsheets is found on the most desktop computers.

The idea behind spreadsheets are very simple, though powerful. A spreadsheet consists of a table where each cell contains either a number or a formula. A formula can compute an expression, the depends on the values of other cells. Text and graphics can is added for presentation purposes.

You is to write a very simple spreadsheet application. Your program should accept several spreadsheets. Each cell of the spreadsheet contains either a numeric value (integers only) or a formula, and which only support sums. After have computed the values of all formulas, your program should output the resulting spreadsheet where all formulas has been replaced by their value.
A1    B1     C1     D1     E1     F1     ...     A2    B2     C2     D2     E2     F2     ...     A3    B3     C3     D3     E3     F3     ...     A4    B4     C4     D4     E4     F4     ...     A5    B5     C5     D5     E5     F5     ...     A6    B6     C6     D6     E6     F6     ...     ...   ...    ...    ...    ...    ...    ...    

Figure 1:naming of the top left cells

Input

The first line of the input file contains the number of spreadsheets to follow. A spreadsheet starts with a line consisting of both integer numbers, separated by a space, giving the number of columns and Rows. The following lines of the spreadsheet each contain a row. A row consists of the cells of that row, separated by a single space.

A cell consists either of a numeric integer value or of a formula. A formula starts with the equal sign (=). After the, one or more cells names follow, separated by plus signs (+). The value of such a formula is the sum of all values found in the referenced cells. These cells may again contain a formula. There is no spaces within a formula.

Safely assume that there is no cyclic dependencies between cells. So each spreadsheet can is fully computed.

The name of a cell consists of one to three letters for the column followed by a number between 1 and 999 (including) for The row. The letters for the column form the following series:a, B, C, ..., Z, AA, AB, AC, ..., AZ, BA, ..., BZ, CA, ... ZZ, AAA, AAB, AAC, ... Aaz, ABA, ..., ABZ, ACA, ..., ZZZ. These letters correspond to the number from 1 to 18278. The top left cell has the name A1. See Figure 1.

Output

The output of your program should has the same format as the input, except that number of spreadsheets and the number of columns and rows is not repeated. Furthermore, all formulas should is replaced by their value.

Sample Input

310 =a1+b1+c140 =a2+b2+c2=a1+a2 =b1+b2 =c1+c2 =d1+d2

Sample Output

10 34 37 8140 17 34 9150 51 71 172

Source

Southwestern European Regional Contest 1995 test instructions: The name of the column is:A, B, C, ..., Z, AA, AB, AC, ..., AZ, BA, ..., BZ, CA, ... ZZ, AAA, AAB, AAC, ... Aaz, ABA, ..., ABZ, ACA, ..., ZZZ. The name of the row is 1~999. Now give a matrix, each lattice is either a number or an expression (=a1+a2 = number of current lattice = number of first column in first row + number of second column in first row), to find out the number of all squares, to ensure that there is an answer. Problem solving: This is the topological ordering, mainly processing the map, and pay attention to memory allocation.
#include <stdio.h> #include <string> #include <queue> #include <iostream>using namespace std; const int N = 1005;const int M = 26+26*26+26*26*26;int num[n][m],n,m,in[n*m];vector<vector<int> >mapt;void to    Pesort () {queue<int>q;    int s;    for (int i=0;i<n*m;i++) if (in[i]==0) Q.push (i);        while (!q.empty ()) {S=q.front (); Q.pop ();        int len=mapt[s].size ();            for (int i=0;i<len;i++) {int v=mapt[s][i];            in[v]--;            NUM[V/M][V%M]+=NUM[S/M][S%M];        if (in[v]==0) Q.push (v);    }}}void Build (const string &s,int i,int j) {int len=s.length (), ans=0;        if (s[0]>= ' 0 ' &&s[0]<= ' 9 ') {for (int ti=0;ti<len;ti++) ans=ans*10+s[ti]-' 0 ';    Num[i][j]+=ans;        } else {int r=0,l,ti,tj;            while (R<len) {r++; if (s[r]>= ' 0 ' &&s[r]<= ' 9 ') {ans=0;                while (s[r]>= ' 0 ' &&s[r]<= ' 9 ' &&r<len) {ans=ans*10+s[r]-' 0 '; r++;                } Num[i][j]+=ans;            Continue            } l=r;            while (s[r]>= ' A ' &&s[r]<= ' Z ') r++;            if (r-l==1) tj=s[l]-' A ';            else if (r-l==2) tj=26+ (s[l]-' a ') *26+s[l+1]-' a '-1;            else tj=26+26*26+ (s[l]-' a ') *26*26+ (s[l+1]-' a ') *26+s[l+2]-' a '-1;            ti=0;            while (s[r]>= ' 0 ' &&s[r]<= ' 9 ' &&r<len) {ti=ti*10+s[r]-' 0 '; r++;            } ti--; Mapt[ti*m+tj].push_back (I*M+J);    in[i*m+j]++;//each two-dimensional position (I,J) is represented by a number i*m+j}}}int main () {int t;    String str;    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &m,&n);        Mapt=vector<vector<int> > (n*m,vector<int> (0,0));        for (int i=0;i<=n*m;i++) in[i]=0,mapt[i].clear ();    for (int i=0;i<n;i++)    for (int j=0;j<m;j++) num[i][j]=0;            for (int i=0;i<n;i++) for (int j=0;j<m;j++) {cin>>str;        Build (STR,I,J);        } topesort ();            for (int i=0;i<n;i++) {printf ("%d", num[i][0]);            for (int j=1;j<m;j++) printf ("%d", num[i][j]);        printf ("\ n"); }            }}


POJ1420 Spreadsheet (topological sort) Note that hyper-memory

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.