QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#877392#4049. 预处理器RDFZchenyy100 ✓1ms6000kbC++143.1kb2025-01-31 21:50:472025-01-31 21:50:48

Judging History

This is the latest submission verdict.

  • [2025-01-31 21:50:48]
  • Judged
  • Verdict: 100
  • Time: 1ms
  • Memory: 6000kb
  • [2025-01-31 21:50:47]
  • Submitted

answer

#include<bits/stdc++.h>

struct Node{
    int child[128];
    char content[105];
    int endCnt,passCnt;
    int clen;
};

Node t[10005];
int nodeCnt=1;
int newNode(){
    return ++nodeCnt;
}

char name[10005]; 
char content[10005];
char line[10005]; int llen;
int inExp[10005];
int insName(int len,int clen){
    int p=1;
    for(int i=1;i<=len;i++){
        t[p].passCnt++;
        if(!t[p].child[name[i]]) t[p].child[name[i]]=newNode();
        p=t[p].child[name[i]];
    }
    t[p].endCnt++,t[p].passCnt++;
    for(int i=1;i<=clen;i++) t[p].content[i]=content[i];
    t[p].clen=clen;
    return p;
}
void ersName(int len){
    int p=1;
    for(int i=1;i<=len;i++){
        t[p].passCnt--;
        p=t[p].child[name[i]];
    }
    t[p].endCnt--,t[p].passCnt--;
    return;
}
void sub(int l,int r,int id){
    int olen=r-l+1;
    if(t[id].clen>olen){
        int d=t[id].clen-olen;
        for(int i=llen;i>r;i--){
            line[i+d]=line[i];
        }
        llen+=d;
    }else if(t[id].clen<olen){
        int d=olen-t[id].clen;
        for(int i=r+1;i<=llen;i++){
            line[i-d]=line[i];
        }
        llen-=d;
    }
    for(int i=1;i<=t[id].clen;i++) line[l-1+i]=t[id].content[i];
    return;
}
std::pair<int,int> mch(int pos){
    int p=1;
    int res=0;
    int cnt=0,ecnt=0;
    for(int i=pos;i<=llen;i++){
        if(!t[t[p].child[line[i]]].passCnt) break;
        ++cnt;
        p=t[p].child[line[i]];
        if(t[p].endCnt) ecnt=cnt,res=p;
    }
    return std::make_pair(res,ecnt);
}
bool isa(char x){
    return (x>='a'&&x<='z')||(x>='A'&&x<='Z')||(x>='0'&&x<='9')||x=='_';
}

int divide(int l,int r);
int expand(int l,int r);

int divide(int l,int r){
    for(int i=l;i<=r;i++){
        if(isa(line[i])){
            int tmp=i;
            while(i<=r){
                ++i;
                if(!isa(line[i])) break;
            }
            --i;
            int d=expand(tmp,i)-i;
            r+=d,i+=d;
        }
    }
    return r;  
}
int expand(int l,int r){
    std::pair<int,int> tmp=mch(l);
    if(tmp.second==r-l+1&&(!inExp[tmp.first])){
        inExp[tmp.first]=1;
        sub(l,r,tmp.first);
        r=l+t[tmp.first].clen-1;
        int newr=divide(l,r);
        inExp[tmp.first]=0;
        return newr;
    }
    return r;
}

int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(0),std::cout.tie(0);

    int lineN; 
    scanf("%d",&lineN);
    fgets(line,200,stdin);
    for(int i=1;i<=lineN;i++){
        fgets(line+1,200,stdin);
        llen=strlen(line+1);
        --llen;
        if(line[1]!='#'){
            divide(1,llen);
            for(int j=1;j<=llen;j++) std::cout<<line[j];
            std::cout<<'\n';
        }else if(line[2]=='d'){
            int cnt=0;
            for(int i=9;i<=llen;i++){
                if(line[i]!=' ') name[++cnt]=line[i];
                else break;
            }
            int cc=0;
            for(int i=8+cnt+2;i<=llen;i++) content[++cc]=line[i];
            insName(cnt,cc);
            std::cout<<'\n';
        }else{
            int cnt=0;
            for(int i=8;i<=llen;i++) name[++cnt]=line[i];
            ersName(cnt);
            std::cout<<'\n';
        }            
    }

    return 0;
}

詳細信息


Pretests


Final Tests

Test #1:

score: 10
Accepted
time: 0ms
memory: 3968kb

input:

1
printf("Hello, world!\n");

output:

printf("Hello, world!\n");

result:

ok single line: 'printf("Hello, world!\n");'

Test #2:

score: 10
Accepted
time: 0ms
memory: 3840kb

input:

5
#define PI 3.14159
#define E 2.71828
std::cout << PI * r * r << std::endl;
std::cout << log10(x) / log10(E) << std::endl;
std::cout << "E^(PI * i) + 1 = 0" << std::endl;

output:


std::cout << 3.14159 * r * r << std::endl;
std::cout << log10(x) / log10(2.71828) << std::endl;
std::cout << "2.71828^(3.14159 * i) + 1 = 0" << std::endl;

result:

ok 5 lines

Test #3:

score: 10
Accepted
time: 1ms
memory: 3968kb

input:

5
#define A abc
#define B hij
A B AB BA B A
#undef A
A B AB BA B A

output:


abc hij AB BA hij abc

A hij AB BA hij A

result:

ok 5 lines

Test #4:

score: 10
Accepted
time: 0ms
memory: 3840kb

input:

9
#define A B B
A B C
C B A
#define B C C
A B C
C B A
#define C [a, b, c]
A B C
C B A

output:

B B B C
C B B B

C C C C C C C
C C C C C C C

[a, b, c] [a, b, c] [a, b, c] [a, b, c] [a, b, c] [a, b, c] [a, b, c]
[a, b, c] [a, b, c] [a, b, c] [a, b, c] [a, b, c] [a, b, c] [a, b, c]

result:

ok 9 lines

Test #5:

score: 10
Accepted
time: 1ms
memory: 3840kb

input:

6
#define A A+B+C
#define B b+C
#define C B+c
A*B*C
#undef B
A*B*C

output:



A+b+B+c+b+C+c*b+B+c*b+C+c

A+B+B+c*B*B+c

result:

ok 6 lines

Test #6:

score: 10
Accepted
time: 0ms
memory: 3968kb

input:

100
8czu(!@iNcuVcs9Zu3/a.$Fo{Fij<4.'Y}JZ:6>2%evBhvjA7IItz`85q'pJ 80"<+vdDzt`?D9L\Ryx0y2[Lu Viek6-NfJ X}=
b&"Yz8kOQ{?yVq\>C85o*Y,XaEb^?;Cf?{iTg**mSZy?NtY'sD 9PWlvF3vug7v\=gw_um^1S,.rjONR'!~jLF=xrQNAx]P=Ze ;
}^<pTuLfJ~y,QN{ 0&oB}2Y)f2V=o|hr3!wl@x36N2X^t&wL!CD)gR  guy^??Io;%5~9a/C_`mkk0z4&t7yT\<iNAa+7y...

output:

8czu(!@iNcuVcs9Zu3/a.$Fo{Fij<4.'Y}JZ:6>2%evBhvjA7IItz`85q'pJ 80"<+vdDzt`?D9L\Ryx0y2[Lu Viek6-NfJ X}=
b&"Yz8kOQ{?yVq\>C85o*Y,XaEb^?;Cf?{iTg**mSZy?NtY'sD 9PWlvF3vug7v\=gw_um^1S,.rjONR'!~jLF=xrQNAx]P=Ze ;
}^<pTuLfJ~y,QN{ 0&oB}2Y)f2V=o|hr3!wl@x36N2X^t&wL!CD)gR  guy^??Io;%5~9a/C_`mkk0z4&t7yT\<iNAa+7y(ZOa...

result:

ok 100 lines

Test #7:

score: 10
Accepted
time: 0ms
memory: 6000kb

input:

100
#define a ?!*(\@{$.!(;!'?<(=^
V X o i C I H p M A e L O F P x z h b T K s l t u Q E G m d R D c q J w N y f g U n j r v B S k W a
#define b >'[&-~?<@`](!+*(}{+
h g S l A T r N O M C k a w U G H s m E t n v f u e d K b y c W Q o x V B L R I J p i q j D X z F P
#define c }{);'|{*?>&\<];!+!|
X h c ...

output:

V X o i C I H p M A e L O F P x z h b T K s l t u Q E G m d R D c q J w N y f g U n j r v B S k W ?!*(\@{$.!(;!'?<(=^

h g S l A T r N O M C k ?!*(\@{$.!(;!'?<(=^ w U G H s m E t n v f u e d K >'[&-~?<@`](!+*(}{+ y c W Q o x V B L R I J p i q j D X z F P

X h }{);'|{*?>&\<];!+!| z T C P Q M G ?!*(\...

result:

ok 100 lines

Test #8:

score: 10
Accepted
time: 1ms
memory: 3968kb

input:

100
#define b \)&%\<=!$(><[[./*{\
o p R a x m V i F H O X J e K d z Q C G b E M s l t u A W T I N h D c q L w P y f g U n j r v B S k
#define p &-~?<@`](!+*(}{+?-|
g W T N h X y w A C r s b l k a S Q K E m c t n v M R u e H O d f F G U o x z P B L V I J p i q j D
#define W '|{*?>&\<];!+!|{^+"
S V j ...

output:

o p R a x m V i F H O X J e K d z Q C G \)&%\<=!$(><[[./*{\ E M s l t u A W T I N h D c q L w P y f g U n j r v B S k

g W T N h X y w A C r s \)&%\<=!$(><[[./*{\ l k a S Q K E m c t n v M R u e H O d f F G U o x z P B L V I J &-~?<@`](!+*(}{+?-| i q j D

S V j P &-~?<@`](!+*(}{+?-| O g X R y T D C...

result:

ok 100 lines

Test #9:

score: 10
Accepted
time: 0ms
memory: 4096kb

input:

100
#define b X*
E x X l q u r h J t j H P S D O d v U e C k Q w M y a z V s F K A m N G W n T b c B L f I R g i o p
#define N T>
p V E q b j A O N l d a z R G J C i X P I h F W B t U u v L H M n w K k x D r y g S f s o Q c e T m
#define P X&
K U y Q g A V l b D E e w s k H a r P S M C N m c t n v O...

output:

E x X l q u r h J t j H P S D O d v U e C k Q w M y a z V s F K A m N G W n T X* c B L f I R g i o p

p V E q X* j A O T> l d a z R G J C i X P I h F W B t U u v L H M n w K k x D r y g S f s o Q c e T m

K U y Q g A V l X* D E e w s k H a r X& S M C T> m c t n v O T u j h d f F G W o x z R B L X I...

result:

ok 100 lines

Test #10:

score: 10
Accepted
time: 1ms
memory: 4096kb

input:

100
#define b V*
E x X l q u r h J t j H P S D O d v U e C k Q w M y a z V s F K A m N G W n T b c B L f I R g i o p
#define N x>
p V E q b j A O N l d a z R G J C i X P I h F W B t U u v L H M n w K k x D r y g S f s o Q c e T m
#define P D&
K U y Q g A V l b D E e w s k H a r P S M C N m c t n v O...

output:

E x X l q u r h J t j H P S D O d v U e C k Q w M y a z V s F K A m N G W n T V* c B L f I R g i o p

p V E q V* j A O x> l d a z R G J C i X P I h F W B t U u v L H M n w K k x D r y g S f s o Q c e T m

K U y Q g A V l V* D E e w s k H a r D& S M C x> m c t n v O T u j h d f F G W o x z R B L X I...

result:

ok 100 lines

Extra Test:

score: 0
Extra Test Passed