顯示具有 output 標籤的文章。 顯示所有文章
顯示具有 output 標籤的文章。 顯示所有文章

2012年9月14日 星期五

uva 391 - Makr-up



 Mark-up 

Mark-up languages are computer languages that assist in the formatting of text files. Special keywords are used to mark the text to allow control of fonts, page styles, paragraph styles, etc. TeX, troff, and HTML are examples of mark-up languages.

Spell checking can be difficult to adapt to these special texts. In general, special processors or spell checkers must be created in order to accommodate mark-up languages. A special processor would recognize the mark-up language and strip it from the text so that the ``plain'' text could then be processed by a spell checker. For this problem, you are to write such a processor for a small mark-up language so that the output of your program will be the plain text without the mark-ups.

The mark-up language to consider is one which allows the modification of fonts within the text. Each markup command will be preceded by a \ character. If the letter following the \ character is not a recognized command from the table below then the character following the \ is printed as part of the plain text. For instance, the mark-up \\ can be used to print a single \.

tex2html_wrap_inline31 b
toggle bold font on/off (default state is off)
tex2html_wrap_inline31 i
toggle italics font on/off (default state is off)
tex2html_wrap_inline31 s
set font size; the s is immediately followed by an optional number; if the number is missing then the command will restore the previous size
tex2html_wrap_inline31 *
toggle processing of mark-ups on/off; if processing is toggled off then mark-ups are considered to be literal text (default state is on)
The number following the SPMamp& command can have a decimal point so 12, 9.5, 11., and .5 should all be recognized as valid numbers.

Input and Output

The input file will be plain text containing mark-ups from the language above. At the start, processing of mark-ups should be on. The file should be processed until the end-of-file is encountered.

Sample Input


\s18.\bMARKUP sample\b\s

\*For bold statements use the \b command.\*

If you wish to \iemphasize\i something use the \\i command.

For titles use \s14BIG\s font sizes, 14 points usually works well.

Remember that all of the commands toggle except for the \\s command.

Sample Output


MARKUP sample

For bold statements use the \b command.

If you wish to emphasize something use the \i command.

For titles use BIG font sizes, 14 points usually works well.

Remember that all of the commands toggle except for the \s command.

output

#include<stdio.h>
#include<cstring>
#include<string>
#define REP(i, b, n) for (int i = b; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define DBG 0
using namespace std;

bool markup;
string cmd;
string getCmd(int &cur){
if(DBG)printf("getcmd %d\n",cur);
if(cmd[cur]=='\\'){
if(!markup)
if(cur+1>=cmd.size()||cmd[cur+1]!='*'){cur+=1;return "\\";} //go one letter
else{markup=1,cur+=2;return "";}
if(cmd[cur+1]=='\\'){cur+=2;return "\\";}
else if(cmd[cur+1]=='b'||cmd[cur+1]=='i'){cur+=2;}
else if(cmd[cur+1]=='*'){cur+=2,markup=!markup;}
else if(cmd[cur+1]=='s'){
int c=cur+2;
bool pp=0;
while(c<cmd.size()&&isdigit(cmd[c]))c++;
if(c<cmd.size()&&cmd[c]=='.')pp=1,c++;
while(c<cmd.size()&&isdigit(cmd[c]))c++;
if(DBG)printf("get num %s\n",cmd.substr(cur+2,c-cur-2).c_str());
if(c-cur==3&&pp)cur+=2; //only .
else cur+=c-cur;
}
else cur++; //useless
return "";
}
else{
int c=cur;
while(cur<cmd.size()&&cmd[cur]!='\\')cur++;
return cmd.substr(c,cur-c);
}
}
void ans(){
int cur=0;
string next,res;
while(1){
if(cur>=cmd.size())break;
next=getCmd(cur);
if(DBG)printf("next: %s\n",next.c_str());
res+=next;
}
printf("%s\n",res.c_str());
}
int main(){
char chs[1024];
markup=1;
while(fgets(chs,1024,stdin)){
if(chs[0]=='\n'){printf("\n");continue;}
if(chs[strlen(chs)-1]=='\n')chs[strlen(chs)-1]='\0';
cmd=chs;
ans();
}
}

uva 337 - 337 - Interpreting Control Sequences



 Interpreting Control Sequences 

Virtually all text-mode terminals are special-purpose computer systems, including a serial port (for communication with a modem or another computer system), a keyboard, a CRT, and of course, a microprocessor, some RAM, and a control program in ROM.
When a character arrives at the terminal, either from the keyboard or the serial port, the terminal's software classifies it as either a display character (which is to be displayed on the CRT) or as a character that introduces a control sequence. A control sequence is used to direct the terminal to do such things as clear the screen, move the cursor in a specified manner, or perhaps change fonts.
In this problem assume you are writing the software for a small terminal with a 10-row, 10-column display (perhaps for a point-of-sale terminal). Rows and columns are numbered 0 through 9. The character that introduces a control sequence is tex2html_wrap_inline39, the circumflex. The character (or in one case, the two characters) immediately following the control sequence introducer will direct your software in performing its special functions. Here is the complete list of control sequences you will need to interpret:

tex2html_wrap_inline39 b
Move the cursor to the beginning of the current line; the cursor row does not change
tex2html_wrap_inline39 c
Clear the entire screen; the cursor row and column do not change
tex2html_wrap_inline39 d
Move the cursor down one row if possible; the cursor column does not change
tex2html_wrap_inline39 e
Erase characters to the right of, and including, the cursor column on the cursor's row; the cursor row and column do not change
tex2html_wrap_inline39 h
Move the cursor to row 0, column 0; the image on the screen is not changed
tex2html_wrap_inline39 i
Enter insert mode (see below)
tex2html_wrap_inline39 l
Move the cursor left one column, if possible; the cursor row does not change
tex2html_wrap_inline39 o
Enter overwrite mode (see below)
tex2html_wrap_inline39 r
Move the cursor right one column, if possible; the cursor row does not change
tex2html_wrap_inline39 u
Move the cursor up one row, if possible; the cursor column does not change
tex2html_wrap_inline61
Write a circumflex ( tex2html_wrap_inline39 ) at the current cursor location, exactly as if it was not a special character; this is subject to the actions of the current mode (insert or overwrite)
tex2html_wrap_inline39 ##
Move the cursor to the row and column specified; # represents a decimal digit; the first # represents the new row number, and the second # represents the new column number
No illegal control sequences will ever be sent to the terminal. The cursor cannot move outside the allowed screen locations (that is, between row 0, column 0 and row 9, column 9).

When a normal character (not part of a control sequence) arrives at the terminal, it is displayed on the terminal screen in a manner that depends on the terminal mode. When the terminal is in overwrite mode (as it is when it is first turned on), the received character replaces the character at the cursor's location. But when the terminal is in insert mode, the characters to the right of and including the cursor's location are shifted right one column, and the new character is placed at the cursor's location; the character previously in the rightmost column of the cursor's row is lost. Regardless of the mode, the cursor is moved right one column, if possible.

Input

The input will contain multiple tests of your terminal software. Each test begins with a line containing an integer N. Following this line there will be N more lines of data, each character of which is to be treated as if it was input, in the order read, to your terminal software. There will be no tab characters in the input data, and ends of lines in the input are to be ignored. Note that blanks in the input data are normal characters to be displayed on your terminal's screen. The last test will be followed by a single line containing the integer 0. No control sequence will be split between two lines of the input data.

At the beginning of each test case you are to assume the terminal screen is clear (that is, filled with blanks), that the terminal is in overwrite mode, and that the cursor is in row 0, column 0 of the screen.

Output

For each input test case, output a line with the case number (these are numbered sequentially starting with 1) and the screen image the way it would look at the end of processing the data in the test case. Enclose the screen image in a "box;" see the sample below for illustration of the required format.

Sample Input


7
This is bad^h^c
^05^^
^14/ \^d^b   /   \
^u^d^d^l^l^l^l^l^l^l^l^l
^r^r< ACM >^l^l^d/^b   \
^b^d    \ /
^d^l^lv
7
^i9^l8^l7^l6^l5^l4^l3^l2^l1^l0
^o^d^lThis is #1^d^bThis is #2
^d^bThis is #3^d^bThis is #4
^d^bThis is #5^d^bThis is #6
^d^bThis is #7^d^bThis is #8
^i^d^bThis is #9^d^bThis is #10
^54^e Hello^d^l^l^l^lWorld
0

Sample Output


Case 1
+----------+
|     ^    |
|    / \   |
|   /   \  |
|  < ACM > |
|   \   /  |
|    \ /   |
|     v    |
|          |
|          |
|          |
+----------+
Case 2
+----------+
|0123456789|
|This is #1|
|This is #2|
|This is #3|
|This is #4|
|This Hello|
|This World|
|This is #7|
|This is #8|
|This is #0|
+----------+

output
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<string>
#include<map>
#include<sstream>
#include<iostream>
#define REP(i, b, n) for (int i = b; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define DBG 0
using namespace std;

int CR,CC,dir[4][2],cases=1;
string screen[10];
bool mode;
char cd[128];
string cmd;
void init(){
    dir[0][0]=-1,dir[1][1]=1,dir[2][0]=1,dir[3][1]=-1;
    cd['u']=0,cd['r']=1,cd['d']=2,cd['l']=3;
}
string getCmd(int &cur,int &type){
    string cd;
    if(cmd[cur]=='^'){
        type=1;
        if(isdigit(cmd[cur+1])){cd=cmd.substr(cur,3),cur+=3;return cd;}
        else{cd=cmd.substr(cur,2),cur+=2;return cd;}
    }
    else{
        type=2;
        int c=cur;
        while(cur<cmd.size()&&cmd[cur]!='^')cur++;
        return cmd.substr(c,cur-c);
    }
}
void append(string a){
    if(!mode){
        screen[CR].insert(CC,a);
        if(screen[CR].size()>10)screen[CR]=screen[CR].substr(0,10);
    }
    else{
        REP(i,CC,CC+a.size()){
            if(i>=10)break;
            screen[CR][i]=a[i-CC];
        }
    }
    CC+=a.size();
    if(CC>10)screen[CR][9]=a[a.size()-1];
    if(CC>=10)CC=9;
}
bool isValid(int r,int c){return r>=0&&r<10&&c>=0&&c<10;}
void print(){rep(i,10){printf("+");rep(j,10)printf("%c",screen[i][j]);printf("+\n");}}
void go(){
    int cur=0,type;
    string next;
    while(1){
        next=getCmd(cur,type);
        if(DBG)printf("cmd %s(%d %d)\n",next.c_str(),CR,CC);
        if(DBG)print();
        if(type==1){
            if(next[1]=='h')CR=CC=0;
            else if(next[1]=='b')CC=0;
            else if(next[1]=='c')rep(i,10)screen[i].assign(10,' ');
            else if(next[1]=='d'||next[1]=='u'||next[1]=='l'||next[1]=='r'){
                if(isValid(CR+dir[cd[next[1]]][0],CC+dir[cd[next[1]]][1]))
                    CR+=dir[cd[next[1]]][0],CC+=dir[cd[next[1]]][1];
            }
            else if(next[1]=='e')REP(i,CR,10)screen[CR][i]=' ';
            else if(next[1]=='i')mode=0;
            else if(next[1]=='o')mode=1;
            else if(isdigit(next[1]))CR=next[1]-'0',CC=next[2]-'0';
            else if(next[1]=='^')append("^");
        }
        else append(next);                  //add text
        if(cur>=cmd.size())break;
    }
}
void ans(){
    printf("Case %d\n",cases++);
    printf("+----------+\n");
    rep(i,10){printf("|");printf("%s",screen[i].c_str());printf("|\n");}
    printf("+----------+\n");
}

int main(){
    int a;
    char chs[200];
    init();
    while(scanf("%d ",&a)==1){
        if(a==0)break;
        CR=0,CC=0,mode=1;
        rep(i,10)screen[i].assign(10,' ');
        rep(i,a){
            fgets(chs,200,stdin);
            if(chs[strlen(chs)-1]=='\n')chs[strlen(chs)-1]='\0';
            cmd=chs,go();
        }
        ans();
    }
    
}

2012年9月13日 星期四

uva 320 - Border



 Border 

You are to write a program that draws a border around a closed path into a bitmap, as displayed in the following figure:

The path is closed and runs along the grid lines, i.e. between the squares of the grid. The path runs counter-clockwise, so if following the path is considered as going ``forward'', the border pixels are always to the ``right'' of the path. The bitmap always covers 32 by 32 squares and has its lower left corner at (0,0). You can safely assume that the path never touches the bounding rectangle of the bitmap and never touches or crosses itself. Note that a bit gets set if it is on the outside of the area surrounded by the path and if at least one of its edges belongs to the path, but not if only one of its corners is in the path. (A look at the convex corners in the figure should clarify that statement.)

Input

The first line of the input file contains the number of test cases in the file. Each test case that follows consists of two lines. The first line of each case contains two integer numbers x and y specifying the starting point of the path. The second line contains a string of variable length. Every letter in the string symbolizes a move of length one along the grid. Only the letters`W' (``west''), `E' (``east''), `N' (``north''), `S' (``south''), and `.' (``end of path'', no move) appear in the string. The end-of-path character (`.') is immediately followed by the end of the line.

Output

For each test case, output a line with the number of the case (`Bitmap #1', `Bitmap #2', etc.). For each row of the bitmap from top to bottom, print a line where you print a character for every bit in that row from left to right. Print an uppercase`X' for set bits and a period `.' for unset bits. Output a blank line after each bitmap.

Sample Input


1
2 1
EENNWNENWWWSSSES.

Sample Output


Bitmap #1
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
.XXX............................
X...X...........................
X..X............................
X...X...........................
.X..X...........................
..XX............................

simulation
#include<stdio.h>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<iostream>
#define REP(i, b, n) for (int i = b; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define DBG 0
using namespace std;

int cases=1,R,C,pdir[4][2],dir[4][2],ctd[128];
bool occ[32][32];
vector<char>seq;
void init(){
    pdir[0][1]=-1,pdir[2][0]=-1,pdir[3][0]=-1,pdir[3][1]=-1;
    dir[0][0]=1,dir[1][1]=1,dir[2][0]=-1,dir[3][1]=-1;
    ctd['E']=0,ctd['N']=1,ctd['W']=2,ctd['S']=3;
}
bool isValid(int r,int c){return r>=0&&r<32&&c>=0&&c<32;}
void ans(){
    int x,y,cr=R,cc=C;
    memset(occ,0,sizeof(occ));
    rep(i,seq.size()){
        x=cr+pdir[ctd[seq[i]]][0],y=cc+pdir[ctd[seq[i]]][1];
        if(DBG)printf("cr cc %d %d %d %d\n",cr,cc,x,y);
        if(isValid(x,y))occ[x][31-y]=1;
        cr+=dir[ctd[seq[i]]][0],cc+=dir[ctd[seq[i]]][1];
        if(cr==R&&cc==C)break;
    }
    printf("Bitmap #%d\n",cases++);
    rep(i,32){
        rep(j,32)if(occ[j][i])printf("X");else printf(".");
        printf("\n");
    }
    printf("\n");
}
int main(){
    int n;
    char a;
    init();
    scanf("%d",&n);
    rep(i,n){
        seq.clear();
        scanf("%d%d ",&R,&C);
        if(DBG)printf("R C %d %d\n",R,C);
        while(scanf("%c",&a)==1){
            if(a=='\n'){break;}
            seq.push_back(a);
        }
        ans();
    }
}

uva 312 - Crosswords (II)



 Crosswords (II) 

A crossword can be stored as a matrix tex2html_wrap_inline26 of zeros and ones. Zero represents white squares and one represents black squares. Some squares of the crossword are numbered and assigned to these numbers are the descriptions of the words that should be written either ``across'' or ``down'' into the crossword. A square is numbered if it is a white square and either (a) the square below it is white and there is no white square immediately above, or (b) there is no white square immediately to its left and the square to its right is white. The squares are numbered from left to right, from the top line to the bottom line.
From the matrix a crossword diagram can be drawn. In the diagram each square is represented by a box tex2html_wrap_inline28 characters. Black square and white squares (numbered and not numbered square) are represented as follows (where nnn is the number of the square):

++++++                        ++++++         ++++++
++++++                        +nnn +         +    +
++++++                        +    +         +    +
++++++                        ++++++         ++++++
The remaining characters of the box are spaces. If black squares are given at the edges, they should be removed from the diagram (see the example). Only use spaces as necessary filling characters. Don't use any unnecessary spaces at the end of the line.

Input

The input file consists of several blocks of lines each representing a crossword. Each block starts with the line containing two integers m < 25 and n < 25 separated by one space. In each of the next m lines there are n numbers 0 or 1, separated by one space. The last block will be empty, m = n = 0.

Output

The output file contains the corresponding crossword diagram for each except the last block. After each diagram there is one empty line.

Sample Input


6 7
1 0 0 0 0 1 1
0 0 1 0 0 0 0
0 0 0 0 1 0 0
0 1 0 0 1 1 1
0 0 0 1 0 0 0
1 0 0 0 0 0 1
5 3
1 0 1
0 0 0
1 1 1
0 0 0
1 0 1
0 0

Sample Output


     +++++++++++++++++++++
     +001 +    +002 +003 +
     +    +    +    +    +
++++++++++++++++++++++++++++++++++++
+004 +    ++++++005 +    +006 +007 +
+    +    ++++++    +    +    +    +
++++++++++++++++++++++++++++++++++++
+008 +    +009 +    +    +010 +    +
+    +    +    +    +    +    +    +
+++++++++++++++++++++    +++++++++++
+    ++++++011 +    +
+    ++++++    +    +
++++++++++++++++++++++++++++++++++++
+012 +013 +    ++++++014 +015 +    +
+    +    +    ++++++    +    +    +
++++++++++++++++++++++++++++++++++++
     +016 +    +    +    +    +
     +    +    +    +    +    +
     ++++++++++++++++++++++++++

     ++++++
     +001 +
     +    +
++++++++++++++++
+002 +    +    +
+    +    +    +
++++++++++++++++


++++++++++++++++
+003 +004 +    +
+    +    +    +
++++++++++++++++
     +    +
     +    +
     ++++++

flood-fill, output
#include<stdio.h>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<iostream>
#define REP(i, b, n) for (int i = b; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define DBG 0
using namespace std;

int N,M,dir[4][2],num[25][25],maz[25][25];
bool notprint[25][25];
void init(){
dir[0][0]=-1,dir[1][1]=1,dir[2][0]=1,dir[3][1]=-1;
}
bool isValid(int a,int b){return a>=0&&a<N&&b>=0&&b<M;}
bool isSpace(int a,int b){return isValid(a,b)&&maz[a][b]==0;}
bool isWall(int a,int b){return !isValid(a,b)||maz[a][b]==1;}
bool isOk(int r,int c){
return maz[r][c]==0&&
(isWall(r-1,c)&&isSpace(r+1,c)||isWall(r,c-1)&&isSpace(r,c+1));
}
string ita(int a){
string s;
while(a>0)s+=(a%10+'0'),a/=10;
while(s.size()<3)s+='0';
reverse(s.begin(),s.end());
return s;
}
bool isEdge(int i,int j){return isValid(i,j)&&i==0||j==0||i==N-1||j==M-1;}
void flood(int r,int c){
notprint[r][c]=1;
//if(DBG)printf("fld: %d %d\n",r,c);
int x,y;
rep(i,4){
x=r+dir[i][0],y=c+dir[i][1];
if(isValid(x,y)&&maz[x][y]==1&&!notprint[x][y])flood(x,y);
//else if(DBG)printf("notprint %d %d: %d\n",x,y,notprint[x][y]);
}
}
void print(int num[][25]){rep(i,N){rep(j,M)printf("%d ",num[i][j]);printf("\n");}}
void print(bool num[][25]){rep(i,N){rep(j,M)printf("%d ",num[i][j]);printf("\n");}}
string intersect(string a,string b){
//if(DBG)printf("inter\n%s\n%s\n",a.c_str(),b.c_str());
string s;
rep(i,max(a.size(),b.size()))
if(i<a.size()&&i<b.size())s+=((a[i]=='+'||b[i]=='+')?'+':' ');
else if(i<a.size())s+=a[i];
else s+=b[i];
//if(DBG)printf("res\n%s\n",s.c_str());
return s;
}
void revise(vector<string>&res){
int cur=3;
for(int cur=3;cur+1<res.size();cur+=4){
string s1=res[cur],s2=res[cur+1],s3;
s3=intersect(s1,s2);
res.erase(res.begin()+cur+1),res.erase(res.begin()+cur);
res.insert(res.begin()+cur,s3);
cur--;
}
}
void ans(){
int c=1,d;
int cnum[25];
memset(num,0,sizeof(num));
memset(cnum,-1,sizeof(cnum));
memset(notprint,0,sizeof(notprint));
rep(i,N)rep(j,M)if(isOk(i,j))num[i][j]=c++;
if(DBG){printf("num: \n");print(num);}
rep(i,N)rep(j,M)if(isEdge(i,j)&&maz[i][j]==1&&!notprint[i][j])flood(i,j);
if(DBG){printf("edge: \n");print(notprint);printf("\n");}
rep(i,N)rep(j,M)if(maz[i][j]==0)cnum[i]=j;
vector<string>res;
string s;
rep(i,N){
rep(k,4){
s="";
rep(j,M){
if(!notprint[i][j]){
if(k==0)s+="+++++";
if(k==1)
if(num[i][j])s+="+"+ita(num[i][j])+" ";
else if(maz[i][j]==0)s+="+ ";
else s+="+++++";
if(k==2)if(maz[i][j]==0)s+="+ "; else s+="+++++";
if(k==3)s+="+++++";
}
else if(j<=cnum[i]) //empty cell
if(s.size()&&(!notprint[i][j-1]))s+="+ "; //close
else s+=" ";
else break;
}
if(s.size())s+="+"; //last
if(DBG)printf("s %s\n",s.c_str());
res.push_back(s);
}
}
revise(res);
rep(i,res.size())printf("%s\n",res[i].c_str());
printf("\n");
}
int main(){
int n,c;
init();
while(cin>>N>>M){
if(N==0&&M==0)break;
rep(i,N)rep(j,M)scanf("%d",&maz[i][j]);
ans();
}
}