Spreadsheet Tracking
原題及翻譯
Data in spreadsheets are stored in cells, which are organized in rows ? and columns ?.
電子表格中的數據存儲在單元格中,單元格按行(R)和列(C)組織。
Some operations on spreadsheets can be applied to single cells (r, c), while others can be applied to entire rows or columns.
電子表格上的某些操作可以應用于單個單元格(R、C),而其他操作可以應用于整個行或列。
Typical cell operations include inserting and deleting rows or columns and exchanging cell contents.
典型的單元操作包括插入和刪除行或列以及交換單元內容。
Some spreadsheets allow users to mark collections of rows or columns for deletion, so the entire collection can be deleted at once.
有些電子表格允許用戶標記要刪除的行或列集合,因此可以一次刪除整個集合。
Some (unusual) spreadsheets allow users to mark collections of rows or columns for insertions too.
一些(不尋常的)電子表格也允許用戶標記行或列的集合進行插入。
Issuing an insertion command results in new rows or columns being inserted before each of the marked rows or columns.
發出插入命令將導致在每個標記的行或列之前插入新的行或列。
Suppose, for example, the user marks rows 1 and 5 of the spreadsheet on the left for deletion.
例如,假設用戶將電子表格左側的第1行和第5行標記為刪除。
The spreadsheet then shrinks to the one on the right.
然后電子表格縮小到右邊的那個。
If the user subsequently marks columns 3, 6, 7, and 9 for deletion, the spreadsheet shrinks to this.
如果用戶隨后將第3、6、7和9列標記為刪除,則電子表格將縮小到此列。
If the user marks rows 2, 3 and 5 for insertion, the spreadsheet grows to the one on the left.
如果用戶將第2、3和5行標記為插入,則電子表格將變為左側的一行。
If the user then marks column 3 for insertion, the spreadsheet grows to the one in the middle.
如果用戶隨后將第3列標記為插入,則電子表格將增長到中間的一列。
Finally, if the user exchanges the contents of cell (1,2) and cell (6,5), the spreadsheet looks like the one on the right.
最后,如果用戶交換單元格(1,2)和單元格(6,5)的內容,那么電子表格看起來就像右邊的一樣。
You must write tracking software that determines the final location of data in spreadsheets that result from row, column, and exchange operations similar to the ones illustrated here.
您必須編寫跟蹤軟件來確定數據在電子表格中的最終位置,這些位置是由行、列和交換操作產生的,與此處所示類似。
Input
輸入
The input consists of a sequence of spreadsheets, operations on those spreadsheets, and queries about them.
輸入由一系列電子表格、對這些電子表格的操作以及有關它們的查詢組成。
Each spreadsheet definition begins with a pair of integers specifying its initial number of rows? and columns ?, followed by an integer specifying the number (n) of spreadsheet operations.
每個電子表格定義以一對整數開始,指定其初始行數(r)和列數(c),然后是一個整數,指定電子表格操作的數目(n)。
Row and column labeling begins with 1.
行和列標簽以1開頭。
The maximum number of rows or columns of each spreadsheet is limited to 50.
每個電子表格的最大行數或列數限制為50。
The following n lines specify the desired operations.
以下n行指定所需的操作。
An operation to exchange the contents of cell (r1, c1) with the contents of cell (r2,c2) is given by:EX r1 c1 r2 c2
將單元(R1,C1)的內容物與單元(R2,C2)的內容物交換的操作由:ex r1 c1 r2 c2給出。
The four insert and delete commands—DC (delete columns), DR (delete rows), IC (insert columns), and IR (insert rows) are given by:< command > A x1 x2 . . . xA
四個插入和刪除命令dc(刪除列)、dr(刪除行)、ic(插入列)和ir(插入行)由:a x1 x2給出。…XA
where < command > is one of the four commands; A is a positive integer less than 10, and x1, . . . , xA are the labels of the columns or rows to be deleted or inserted before.
其中是四個命令之一;a是小于10的正整數,x1,。…,xa是要刪除或插入的列或行的標簽。
For each insert and delete command, the order of the rows or columns in the command has no significance.
對于每個插入和刪除命令,命令中的行或列的順序沒有意義。
Within a single delete or insert command, labels will be unique.
在單個刪除或插入命令中,標簽將是唯一的。
The operations are followed by an integer which is the number of queries for the spreadsheet.
操作后面是一個整數,它是電子表格的查詢數。
Each query consists of positive integers r and c, representing the row and column number of a cell in the original spreadsheet.
每個查詢由正整數r和c組成,表示原始電子表格中單元格的行和列號。
For each query, your program must determine the current location of the data that was originally in cell (r, c).
對于每個查詢,程序必須確定原始數據在單元格(r,c)中的當前位置。
The end of input is indicated by a row consisting of a pair of zeros for the spreadsheet dimensions.
輸入的結尾由一行表示,該行由一對用于電子表格維度的零組成。
Output
輸出
For each spreadsheet, your program must output its sequence number (starting at 1).
對于每個電子表格,程序必須輸出其序列號(從1開始)。
For each query, your program must output the original cell location followed by the final location of the data or the word ‘GONE’ if the contents of the original cell location were destroyed as a result of the operations.
對于每個查詢,如果操作導致原始單元格位置的內容被破壞,則程序必須輸出原始單元格位置,后跟數據的最終位置或單詞“Gone”。
Separate output from di?erent spreadsheets with a blank line.
用空行將不同電子表格的輸出分開。
The data file will not contain a sequence of commands that will cause the spreadsheet to exceed the maximum size.
數據文件將不包含將導致電子表格超過最大大小的命令序列。
思路
最直接的思路就是首先模擬操作,算出最后的電子表格,然后在每次查詢時直接在電子表格中找到所求的單元格。
基本思路就是利用三個二維數組和一個一維數組進行轉換:
int r,c,n,d[maxd][maxd],d2[maxd][maxd],ans[maxd][maxd],cols[maxd]; //r——行數,c——列數,n——操作數 //d用于存儲最初的電子表格,d2用于中間進行操作的電子表格,ans用于存儲最終的電子表格代碼分析
#include <stdio.h> #include <string.h> #define maxd 100 #define BIG 10000 int r,c,n,d[maxd][maxd],d2[maxd][maxd],ans[maxd][maxd],cols[maxd]; //r——行數,c——列數,n——操作數 //d用于存儲最初的電子表格,d2用于中間進行操作的電子表格,ans用于存儲最終的電子表格 void copy(char type,int p,int q) {if(type=='R'){//如果是行for(int i=1;i<=c;i++){//讓d第p行的每一列等于d2第q行的每一列d[p][i]=d2[q][i];}}else{//如果是列for(int i=1;i<=r;i++){//讓d第p列的每一行等于d2第q列的每一行d[i][p]=d2[i][q];}} } void del(char type) {memcpy(d2,d,sizeof(d));//從d內存地址的起始位置開始拷貝整個d到d2內存地址中。int cnt=type=='R'?r:c,cnt2=0;//確定刪除的是行/列?for(int i=1;i<=cnt;i++){if(!cols[i]) copy(type,++cnt2,i);//如果該行/列為0,拷貝}if(type=='R') r=cnt2;//如果是行操作,記錄新的行總數else c=cnt2;//如果是列操作,記錄新的列總數 } void ins(char type) {memcpy(d2,d,sizeof(d));int cnt=type=='R'?r:c,cnt2=0;for(int i=1;i<=cnt;i++){if(cols[i]) copy(type,++cnt2,0);copy(type,++cnt2,i);}if(type=='R') r=cnt2;else c=cnt2; } int main () {int r1,c1,r2,c2,q,kase=0;//kase用于輸出標號char cmd[10];memset(d,0,sizeof(d));while(scanf("%d%d%d",&r,&c,&n)==3&&r){//讀入行列數和操作數int r0=r,c0=c;//記錄初始的行和列for(int i=1;i<=r;i++){for(int j=1;j<=c;j++){d[i][j]=i*BIG+j;//先將電子表的每一個單元格初始化}}while(n--){scanf("%s",cmd);//讀入命令if(cmd[0]=='E'){//交換單元格命令scanf("%d%d%d%d",&r1,&c1,&r2,&c2);int t=d[r1][c1];d[r1][c1]=d[r2][c2];d[r2][c2]=t;}else{//如果是插入/刪除命令int a,x;scanf("%d",&a);//a用于存儲要插入/刪除的行/列的個數memset(cols,0,sizeof(cols));//創建一個全為零的行/列for(int i=0;i<a;i++){//用于讀入要執行插入/刪除操作的行/列scanf("%d",&x);//讀入行/列cols[x]=1;//將該行/列設置為1}if(cmd[0]=='D') del(cmd[1]);//如果是刪除操作else ins(cmd[1]);//如果是插入操作}}memset(ans,0,sizeof(ans));//將最終結果的電子表格全部初始化為0for(int i=1;i<=r;i++){for(int j=1;j<=c;j++){ans[d[i][j]/BIG][d[i][j]%BIG]=i*BIG+j;}}if(kase>0) printf("\n");printf("Spreadsheet #%d\n",++kase);scanf("%d",&q);//讀入要查詢的個數while(q--){scanf("%d%d",&r1,&c1);//讀入要查詢的行列數printf("Cell data in (%d,%d) ",r1,c1);if(ans[r1][c1]==0) printf("GONE\n");//if the contents of the original cell location were destroyedelse printf("moved to (%d,%d)\n",ans[r1][c1]/BIG,ans[r1][c1]%BIG);//打印查詢單元格改變后的位置}}return 0; } 與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的Spreadsheet Tracking的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 01、python数据分析与机器学习实战
- 下一篇: Rails