pku2525 Text Formalization 恶心的模拟
题意是这样:将一篇文章中的缩写和简略语替换成原来的样子。
有点要求,缩写只在第一次替换。如果有多种替换方法,则最先出现的可替换词优先替换。如果还有重复,则列表前面的先替换。
解法:
狂用string.h里的函数然后+暴力就可以了,这种模拟题一般不会考到什么算法。。
代码:
1
# include
<
cstdio
>
2 using namespace std;
3 # include < cstring >
4 # include < string >
5 char contraction[ 201 ][ 2 ][ 100 ],acronym[ 51 ][ 2 ][ 100 ];
6 int main()
7 {
8 int c,a;
9 scanf( " %d%d " , & c, & a);
10 getchar();
11 for ( int i = 0 ;i < c;i ++ )
12 {
13 char tmp[ 200 ];
14 gets(tmp);
15 char * p1 = strstr(tmp, " \ "" );
16 char * p2 = strstr(p1 + 1 , " \ "" );
17 * p2 = ' \0 ' ;
18 strcpy(contraction[ 3 * i][ 0 ],p1 + 1 );
19 p1 = strstr(p2 + 1 , " \ "" );
20 p2 = strstr(p1 + 1 , " \ "" );
21 * p2 = ' \0 ' ;
22 strcpy(contraction[ 3 * i][ 1 ],p1 + 1 );
23 strcpy(contraction[ 3 * i + 1 ][ 0 ],contraction[ 3 * i][ 0 ]);
24 strcpy(contraction[ 3 * i + 1 ][ 1 ],contraction[ 3 * i][ 1 ]);
25 contraction[ 3 * i + 1 ][ 0 ][ 0 ] = (contraction[ 3 * i + 1 ][ 0 ][ 0 ] >= ' a ' && contraction[ 3 * i + 1 ][ 0 ][ 0 ] <= ' z ' ? contraction[ 3 * i + 1 ][ 0 ][ 0 ] - 32 :contraction[ 3 * i + 1 ][ 0 ][ 0 ]);
26 contraction[ 3 * i + 1 ][ 1 ][ 0 ] = (contraction[ 3 * i + 1 ][ 1 ][ 0 ] >= ' a ' && contraction[ 3 * i + 1 ][ 1 ][ 0 ] <= ' z ' ? contraction[ 3 * i + 1 ][ 1 ][ 0 ] - 32 :contraction[ 3 * i + 1 ][ 1 ][ 0 ]);
27 strcpy(contraction[ 3 * i + 2 ][ 0 ],contraction[ 3 * i][ 0 ]);
28 strcpy(contraction[ 3 * i + 2 ][ 1 ],contraction[ 3 * i][ 1 ]);
29 for ( int j = 0 ;j < strlen(contraction[ 3 * i + 2 ][ 0 ]);j ++ )
30 contraction[ 3 * i + 2 ][ 0 ][j] = (contraction[ 3 * i + 2 ][ 0 ][j] >= ' a ' && contraction[ 3 * i + 2 ][ 0 ][j] <= ' z ' ? contraction[ 3 * i + 2 ][ 0 ][j] - 32 :contraction[ 3 * i + 2 ][ 0 ][j]);
31 for ( int j = 0 ;j < strlen(contraction[ 3 * i + 2 ][ 1 ]);j ++ )
32 contraction[ 3 * i + 2 ][ 1 ][j] = (contraction[ 3 * i + 2 ][ 1 ][j] >= ' a ' && contraction[ 3 * i + 2 ][ 1 ][j] <= ' z ' ? contraction[ 3 * i + 2 ][ 1 ][j] - 32 :contraction[ 3 * i + 2 ][ 1 ][j]);
33 }
34 c *= 3 ;
35 for ( int i = 0 ;i < a;i ++ )
36 {
37 char tmp[ 200 ];
38 gets(tmp);
39 char * p1 = strstr(tmp, " \ "" );
40 char * p2 = strstr(p1 + 1 , " \ "" );
41 * p2 = ' \0 ' ;
42 strcpy(acronym[i][ 0 ],p1 + 1 );
43 p1 = strstr(p2 + 1 , " \ "" );
44 p2 = strstr(p1 + 1 , " \ "" );
45 * p2 = ' \0 ' ;
46 strcpy(acronym[i][ 1 ],p1 + 1 );
47 }
48 char ostr[ 50000 ];
49 char tstr[ 50000 ];
50 bool used[ 51 ];
51 memset(used, false , sizeof (used));
52 while (gets(ostr))
53 {
54 if (ostr[ 0 ] == ' # ' )
55 {
56 memset(used, false , sizeof (used));
57 puts(ostr);
58 continue ;
59 }
60 char * str = ostr;
61 while ( true )
62 {
63 char * p = NULL, * ori, * target;
64 int type = 0 ;
65 for ( int i = 0 ;i < c;i ++ )
66 {
67 char * tmp = strstr(str,contraction[i][ 0 ]);
68 if (tmp != NULL && (p == NULL || tmp < p))
69 {
70 p = tmp;
71 ori = contraction[i][ 0 ];
72 target = contraction[i][ 1 ];
73 type = 0 ;
74 }
75 }
76 for ( int i = 0 ;i < a;i ++ )
77 {
78 char * tmp = strstr(str,acronym[i][ 0 ]);
79 if (tmp != NULL && (p == NULL || tmp < p))
80 {
81 p = tmp;
82 ori = acronym[i][ 0 ];
83 target = acronym[i][ 1 ];
84 type = i + 1 ;
85 }
86 }
87 if (p == NULL) break ;
88 else
89 {
90 if ( ! type)
91 {
92 * p = ' \0 ' ;
93 strcpy(tstr,str);
94 strcat(tstr,target);
95 strcat(tstr,p + strlen(ori));
96 strcpy(str,tstr);
97 str = p + strlen(target);
98 }
99 else
100 {
101 type -- ;
102 if (used[type]) str += strlen(ori);
103 else
104 {
105 used[type] = true ;
106 * p = ' \0 ' ;
107 strcpy(tstr,str);
108 strcat(tstr,target);
109 strcat(tstr, " ( " );
110 strcat(tstr,ori);
111 strcat(tstr, " ) " );
112 strcat(tstr,p + strlen(ori));
113 strcpy(str,tstr);
114 str = p + strlen(target) + 3 + strlen(ori);
115 }
116 }
117 }
118 }
119 puts(ostr);
120 }
121 return 0 ;
122
123 }
2 using namespace std;
3 # include < cstring >
4 # include < string >
5 char contraction[ 201 ][ 2 ][ 100 ],acronym[ 51 ][ 2 ][ 100 ];
6 int main()
7 {
8 int c,a;
9 scanf( " %d%d " , & c, & a);
10 getchar();
11 for ( int i = 0 ;i < c;i ++ )
12 {
13 char tmp[ 200 ];
14 gets(tmp);
15 char * p1 = strstr(tmp, " \ "" );
16 char * p2 = strstr(p1 + 1 , " \ "" );
17 * p2 = ' \0 ' ;
18 strcpy(contraction[ 3 * i][ 0 ],p1 + 1 );
19 p1 = strstr(p2 + 1 , " \ "" );
20 p2 = strstr(p1 + 1 , " \ "" );
21 * p2 = ' \0 ' ;
22 strcpy(contraction[ 3 * i][ 1 ],p1 + 1 );
23 strcpy(contraction[ 3 * i + 1 ][ 0 ],contraction[ 3 * i][ 0 ]);
24 strcpy(contraction[ 3 * i + 1 ][ 1 ],contraction[ 3 * i][ 1 ]);
25 contraction[ 3 * i + 1 ][ 0 ][ 0 ] = (contraction[ 3 * i + 1 ][ 0 ][ 0 ] >= ' a ' && contraction[ 3 * i + 1 ][ 0 ][ 0 ] <= ' z ' ? contraction[ 3 * i + 1 ][ 0 ][ 0 ] - 32 :contraction[ 3 * i + 1 ][ 0 ][ 0 ]);
26 contraction[ 3 * i + 1 ][ 1 ][ 0 ] = (contraction[ 3 * i + 1 ][ 1 ][ 0 ] >= ' a ' && contraction[ 3 * i + 1 ][ 1 ][ 0 ] <= ' z ' ? contraction[ 3 * i + 1 ][ 1 ][ 0 ] - 32 :contraction[ 3 * i + 1 ][ 1 ][ 0 ]);
27 strcpy(contraction[ 3 * i + 2 ][ 0 ],contraction[ 3 * i][ 0 ]);
28 strcpy(contraction[ 3 * i + 2 ][ 1 ],contraction[ 3 * i][ 1 ]);
29 for ( int j = 0 ;j < strlen(contraction[ 3 * i + 2 ][ 0 ]);j ++ )
30 contraction[ 3 * i + 2 ][ 0 ][j] = (contraction[ 3 * i + 2 ][ 0 ][j] >= ' a ' && contraction[ 3 * i + 2 ][ 0 ][j] <= ' z ' ? contraction[ 3 * i + 2 ][ 0 ][j] - 32 :contraction[ 3 * i + 2 ][ 0 ][j]);
31 for ( int j = 0 ;j < strlen(contraction[ 3 * i + 2 ][ 1 ]);j ++ )
32 contraction[ 3 * i + 2 ][ 1 ][j] = (contraction[ 3 * i + 2 ][ 1 ][j] >= ' a ' && contraction[ 3 * i + 2 ][ 1 ][j] <= ' z ' ? contraction[ 3 * i + 2 ][ 1 ][j] - 32 :contraction[ 3 * i + 2 ][ 1 ][j]);
33 }
34 c *= 3 ;
35 for ( int i = 0 ;i < a;i ++ )
36 {
37 char tmp[ 200 ];
38 gets(tmp);
39 char * p1 = strstr(tmp, " \ "" );
40 char * p2 = strstr(p1 + 1 , " \ "" );
41 * p2 = ' \0 ' ;
42 strcpy(acronym[i][ 0 ],p1 + 1 );
43 p1 = strstr(p2 + 1 , " \ "" );
44 p2 = strstr(p1 + 1 , " \ "" );
45 * p2 = ' \0 ' ;
46 strcpy(acronym[i][ 1 ],p1 + 1 );
47 }
48 char ostr[ 50000 ];
49 char tstr[ 50000 ];
50 bool used[ 51 ];
51 memset(used, false , sizeof (used));
52 while (gets(ostr))
53 {
54 if (ostr[ 0 ] == ' # ' )
55 {
56 memset(used, false , sizeof (used));
57 puts(ostr);
58 continue ;
59 }
60 char * str = ostr;
61 while ( true )
62 {
63 char * p = NULL, * ori, * target;
64 int type = 0 ;
65 for ( int i = 0 ;i < c;i ++ )
66 {
67 char * tmp = strstr(str,contraction[i][ 0 ]);
68 if (tmp != NULL && (p == NULL || tmp < p))
69 {
70 p = tmp;
71 ori = contraction[i][ 0 ];
72 target = contraction[i][ 1 ];
73 type = 0 ;
74 }
75 }
76 for ( int i = 0 ;i < a;i ++ )
77 {
78 char * tmp = strstr(str,acronym[i][ 0 ]);
79 if (tmp != NULL && (p == NULL || tmp < p))
80 {
81 p = tmp;
82 ori = acronym[i][ 0 ];
83 target = acronym[i][ 1 ];
84 type = i + 1 ;
85 }
86 }
87 if (p == NULL) break ;
88 else
89 {
90 if ( ! type)
91 {
92 * p = ' \0 ' ;
93 strcpy(tstr,str);
94 strcat(tstr,target);
95 strcat(tstr,p + strlen(ori));
96 strcpy(str,tstr);
97 str = p + strlen(target);
98 }
99 else
100 {
101 type -- ;
102 if (used[type]) str += strlen(ori);
103 else
104 {
105 used[type] = true ;
106 * p = ' \0 ' ;
107 strcpy(tstr,str);
108 strcat(tstr,target);
109 strcat(tstr, " ( " );
110 strcat(tstr,ori);
111 strcat(tstr, " ) " );
112 strcat(tstr,p + strlen(ori));
113 strcpy(str,tstr);
114 str = p + strlen(target) + 3 + strlen(ori);
115 }
116 }
117 }
118 }
119 puts(ostr);
120 }
121 return 0 ;
122
123 }