CCF 认证

题意:字符串替换

string+map的应用

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 #include<memory.h>
 5 #include<string.h>
 6 #include<algorithm>
 7 #include<cmath>
 8 #include<map>
 9 #define clc(a,b) memset(a,b,sizeof(a))
10 typedef long double ld;
11 typedef long long ll;
12 const int N = 30;
13 const double eps=1e-9;
14 const int inf=-100000;
15 const int maxn=1e5+10;
16 const double Pi=acos(-1);
17 using namespace std;
18 
19 int main()
20 {
21     string s[105];
22     string s1,s2,s3,s4;
23     int n,m;
24     int j1[20],j2[20],k1;
25     cin>>n>>m;
26     getchar();
27     for(int i=0; i<n; i++)
28         getline(cin,s[i]);
29     map<string,string>v;
30     for(int j=0; j<m; j++)
31     {
32         cin>>s1;
33         getchar();
34         getline(cin,s2);
35         s1.insert(0,"{{ ");
36         s1=s1+" }}";
37         s2.erase(0,1);
38         s2.erase(s2.end()-1,s2.end());
39         v[s1]=s2;
40 
41     }
42     for(int i=0; i<n; i++)
43     {
44         k1=0;
45         clc(j1,-1);
46         clc(j2,-1);
47         for(int j=0; s[i][j]!=0; j++)
48         {
49             if(s[i][j]=='{'&&s[i][j+1]=='{')
50                 j1[k1]=j;
51             if(s[i][j]=='}'&&s[i][j+1]=='}')
52                 j2[k1]=j+1;
53             if(j1[k1]!=-1&&j2[k1]!=-1)
54                 k1++;
55         }
56         int t=0;
57         for(int j=0; j<k1; j++)
58         {
59             s3="";
60             for(int k=j1[j]+t; k<=j2[j]+t; k++)
61                 s3.insert(s3.end(),s[i][k]);
62             if(v.count(s3))
63             {
64                 s[i].replace(s[i].begin()+j1[j]+t,s[i].begin()+j2[j]+1+t,v[s3]);
65                 t=t+v[s3].size()-s3.size();
66             }
67             else
68             {
69                 s[i].replace(s[i].begin()+j1[j]+t,s[i].begin()+j2[j]+1+t,"");
70                 t=t-s3.size();
71             }
72         }
73         cout<<s[i]<<endl;
74     }
75     return 0;
76 }
View Code

 

posted @ 2015-12-19 21:04  yyblues  阅读(248)  评论(0编辑  收藏  举报