sdut 3567 Memory Leak 模拟

来源:互联网 发布:识汝不识丁网络剧资源 编辑:程序博客网 时间:2024/06/05 02:55

题意:

模拟c++的输入,如果输入的字符串大于声明的空间就会寻找下一个字符串直到找到'\0'

坑点:gets name 的第一个空格后都是输入的内容

ACcode:

#include <bits/stdc++.h>using namespace std;#define maxn 11111string name[maxn];int len1[maxn],len2[maxn];char tmp[maxn];map<string ,int>vis;int main(){    int loop;    scanf("%d",&loop);    while(loop--){        scanf("%s",tmp);        vis.clear();        memset(len1,0,sizeof(len1));        memset(len2,0,sizeof(len2));        for(int i=0;i<maxn;++i)name[i]="";        int tot=1;        while(1){            scanf("%s",tmp);            int len=strlen(tmp);            name[tot]="";            bool flag=true;            int k;            for(k=0;flag&&k<len;++k){                if(tmp[k]!='[')                    name[tot]+=tmp[k];                else flag=false;            }            len1[tot]=0;            flag=true;            for(;flag&&k<len;++k)                if(isdigit(tmp[k]))                    len1[tot]=len1[tot]*10+tmp[k]-'0';                else flag=false;            vis[name[tot]]=tot;            if(tmp[k]==';')break;            tot++;        }        while(scanf("%s",tmp)&&tmp[0]!='r'){            if(tmp[0]=='g'){                scanf("%s",tmp);                int pos=vis[tmp];                gets(tmp);                int ll=strlen(tmp);                name[pos]="";                int k=1,lll=0;                for(lll=0;k<ll;++k,++lll)                    name[pos]+=tmp[k];                name[pos][lll]='\0';                len2[pos]=lll;            }else {                scanf("%s",tmp);                int pos=vis[tmp];                if(len2[pos]==0)cout<<'\12';                else{                    if(len1[pos]>len2[pos])                        cout<<name[pos]<<'\12';                    else {                        for(int i=0;i<len1[pos];++i)cout<<name[pos][i];                        int k=pos+1;                        while(len1[k]<=len2[k]&&len2[k]>0){                            for(int i=0;i<len1[k];++i)cout<<name[k][i];                            k++;                        }                        if(len2[k])                            cout<<name[k]<<'\12';                        else cout<<'\12';                    }                }            }        }        scanf("%s",tmp);    }    return 0;}/*1char a[2], b[1], c[1], d[111];gets a        1gets b 2c agets a 11c agets c 33c agets b 22c ac breturn 0;4char a[1], b[1], c[12];gets a 123cout areturn 0;char a[1], b[2], c[12];gets a 123cout agets b 123cout acout bgets b 12cout acout bretrun 0;char a[1], b[2], c[12];gets a 12345gets c 12cout agets b 12cout acout bcout creturn 0;char a[1], b[2], c[12];gets a 12345gets c 12cout agets b 1cout acout bcout creturn 0;*/


0 0
原创粉丝点击