这题是基础题,主要是练习编程能力,没有什么算法。。
饶是如此我也花了不少时间。
果如学长所说,A题的过程即痛苦又快乐。
========================================
刚才看了下网上别人贴的代码,发现都很短。。
我知道我这方法又土到家了肯定
sprintf() 和 strcat()函数的应用的确能让程序好写很多
之前我怎么没想到呢。。
土人如我这样的,先找看能否在find里找到匹配的子串,
然后找到后再拿这个串和replace_by里的比较,看是=还是大于或小于
分三种情况插入。
想想头都大了
/**
* Author: Gneveek
* Data: 2011-10-4
* Descripition: UVa 10115 - Automatic Editing
*/
#include <stdio.h>
#include <string.h>
#define LINE 20
#define MAXN 128
#define N 512
int is_exist(char s[], char substring[]);
void replace_by(char s[], int location, int len, char subs[]);
int main()
{
//freopen("autoedit.in","r",stdin);
//freopen("autodeit.out","w",stdout);
//freopen("C:\\in.txt","r",stdin);
int n;
char find_[LINE][MAXN],replace_[LINE][MAXN];
char cas[N];
while(1)
{
// read data :
scanf("%d",&n);
if(0 == n)
break;
getchar();
for(int i=0; i<n; i++)
{
gets(find_[i]);
gets(replace_[i]);
}
gets(cas);
//edit process
for(int line = 0; line < n; line++)
{
int location;
while((location = is_exist(cas, find_[line])) != -1)
{
replace_by(cas,location,strlen(find_[line]),replace_[line]);
}
}
printf("%s\n",cas);
}
return 0;
}
int is_exist(char s[], char substring[])
{
int s_len = strlen(s);
int sub_len = strlen(substring);
int i,j;
for(i=0; i<s_len; i++)
{
if(s[i] == substring[0])
{
for(j=1; j<sub_len; j++)
if(s[i+j] != substring[j])
break;
if(j == sub_len)
return i;
}
}
return -1;
}
void replace_by(char s[], int location, int len, char subs[])
{
int sub_len = strlen(subs);
int i;
if(sub_len == len)
{
for(i=location; i<location+len; i++)
s[i] = subs[i-location];
}
else if(sub_len > len)
{
int sub = sub_len - len;
for(i=strlen(s); i>=location + len; i--)
s[i+sub] = s[i];
for(i=location; i<location + sub_len; i++)
s[i] = subs[i-location];
}
else
{
int sub = len - sub_len;
for(i=location+len; i<strlen(s)+1; i++)
s[i-sub] = s[i];
for(i=location; i<location + sub_len; i++)
s[i] = subs[i-location];
}
}
=======================================
发现一个PASCAL写的。。
var n,i,tmp:longint;
before,after:array[1..10]of string;
txt:string;
begin
while true do begin
readln(n);
if n=0 then break;
for i:=1 to n do begin
readln(before[i]);
readln(after[i]);
end;
readln(txt);
for i:=1 to n do
while pos(before[i],txt)<>0 do begin
tmp:=pos(before[i],txt);
delete(txt,tmp,length(before[i]));
insert(after[i],txt,tmp);
end;
writeln(txt);
end;
end.
好短