当前位置:编程问答 > C/C++ >

uva_10115-Automatic Editing

[cpp] 
/**本题含义是将前一个串转化为后一个串,当前一个串
 *完全找不到后,再去比较下一个串,直到所有串被替换完毕
 */ 
#include <cstdio> 
#include <iostream> 
#include <cstring> 
using namespace std; 
 
#define MAX 256 
 
//替换 
void replace(char *a, char *b, int pos) { 
    for(int i=0; i<strlen(a); i++) { 
        b[pos++] = a[i]; 
    } 

 
//查找子串 
int findSon(char *s1, char *s2, int &start, int &end) { 
    int j, i1; 
    for(int i=0; i<strlen(s2); i++) { 
        i1 = i; 
        for(j=0; j<strlen(s1); j++) { 
            if(s1[j]!=s2[i1++]) break; 
        } 
        if(j==strlen(s1)) { 
            start = i; end = i+j-1; return 1; 
        } 
    } 
    return 0; 

 
 
int main(int argc, char const *argv[]) { 
    int cas, start, end, len, flag; 
    char data[MAX]; 
    char word[MAX][MAX]; 
    while(cin>>cas,cas!=0) { 
        getchar(); 
        for(int i=0; i<2*cas; i+=2) { 
            gets(word[i]); 
            gets(word[i+1]); 
        } 
        gets(data); 
        for(int i=0; i<2*cas; i+=2) { 
            flag = 1; 
            while(flag) { 
                flag = findSon(word[i], data, start, end); 
                len = strlen(word[i])-strlen(word[i+1]); 
                //flag标识是否还存在可替换的串,下面代码为字符串替换,没有使用库函数 
                if(flag) { 
                    if(len<0) { 
                        len = -len; 
                        for(int i=strlen(data); i>end; i--) { 
                            data[i+len] = data[i]; 
                        } 
                    } else if(len>0) { 
                        for(int i=end-len+1; i<strlen(data); i++) { 
                            data[i] = data[i+len]; 
                        } 
                    } 
                    replace(word[i+1], data, start); 
                } 
            } 
 
        } 
        puts(data); 
    } 
    return 0; 

补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,