-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathname.c
142 lines (110 loc) · 3.36 KB
/
name.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* name.c
Corporate name changing program -- string example
by:Steven Skiena
begun: January 28, 2002
revised: July 3, 2002
*/
/*
Copyright 2003 by Steven S. Skiena; all rights reserved.
Permission is granted for use in non-commerical applications
provided this copyright notice remains intact and unchanged.
This program appears in my book:
"Programming Challenges: The Programming Contest Training Manual"
by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003.
See our website www.programming-challenges.com for additional information.
This book can be ordered from Amazon.com at
http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/
*/
#include <stdio.h>
#include <string.h>
#define MAXLEN 1001 /* longest possible string */
#define MAXCHANGES 101 /* maximum number of name changes */
typedef char string[MAXLEN];
string mergers[MAXCHANGES][2]; /* store before/after corporate names */
int nmergers; /* the number of different name changes */
void read_quoted_string(char *s) {
int i = 0; /* counter */
char c; /* latest character */
while ((c = getchar()) != '\"')
;
while ((c = getchar()) != '\"') {
s[i] = c;
i = i + 1;
}
s[i] = '\0';
}
void read_changes(void) {
int i; /* counter */
scanf("%d\n", &nmergers);
for (i = 0; i<nmergers; i++) {
read_quoted_string((char*)&(mergers[i][0]));
read_quoted_string((char*)&(mergers[i][1]));
}
}
/* Replace the substring of length xlen starting at position pos in
string s with the contents of string y.
*/
void replace_x_with_y(char *s, int pos, int xlen, char *y) {
int i; /* counter */
int slen,ylen; /* lengths of relevant strings */
slen = strlen(s);
ylen = strlen(y);
if (xlen >= ylen) {
for (i = (pos+xlen); i <= slen; i++) {
s[i+(ylen-xlen)] = s[i];
}
} else {
for (i = slen; i >= (pos+xlen); i--) {
s[i+(ylen-xlen)] = s[i];
}
}
for (i = 0; i < ylen; i++) {
s[pos+i] = y[i];
}
}
/* Return the position of the first occurrence of the pattern p
in the text t, and -1 if it does not occur.
*/
/* [[[ findmatch_cut */
int findmatch(char *p, char *t) {
int i, j; /* counters */
int plen, tlen; /* string lengths */
plen = strlen(p);
tlen = strlen(t);
for (i = 0; i <= (tlen-plen); i = i + 1) {
j=0;
while ((j < plen) && (t[i + j] == p[j])) {
j = j + 1;
}
if (j == plen) {
return(i);
}
}
return(-1);
}
/* ]]] */
int main(void) {
string s; /* input string */
char c; /* input character */
int nlines; /* number of lines in text */
int i,j; /* counters */
int pos; /* position of pattern in string */
read_changes();
scanf("%d\n", &nlines);
for (i = 1; i <= nlines; i = i + 1) {
/* read text line */
j = 0;
while ((c = getchar()) != '\n') {
s[j] = c;
j = j + 1;
}
s[j] = '\0';
for (j = 0; j < nmergers; j = j + 1) {
while ((pos=findmatch(mergers[j][0], s)) != -1) {
replace_x_with_y(s, pos, strlen(mergers[j][0]), mergers[j][1]);
}
}
printf("%s\n", s);
}
return 0;
}