字符串替换 简单题

13 篇文章 0 订阅
2 篇文章 0 订阅

开学的两小时的比赛,这样一道简单题都被卡了一个多小时,真是。。。没能够熟练的应用stl,导致容易错而且程序异常复杂,不易调试,用stl代码简单容易,何乐而不为呢

A - A
Time Limit:3000MS    Memory Limit:0KB    64bit IO Format:%lld & %llu

Description

Text-processing tools like awk and sed allow you to automatically perform a sequence of editing operations based on ascript. For this problem we consider the specific case in which we want to perform a series of string replacements, within a single line of text, based on a fixed set of rules. Each rule specifies the string to find, and the string to replace it with, as shown below.

RuleFindReplace-by
1.ban bab
2.baba be
3.ana any
4.ba b hind the g

To perform the edits for a given line of text, start with the first rule. Replace the first occurrence of thefind string within the text by the replace-by string, then try to perform the same replacementagain on the new text. Continue until the find string no longer occurs within the text, and then move on to the next rule. Continue until all the rules have been considered. Note that (1) when searching for afind string, you always start searching at the beginning of the text, (2) once you have finished using a rule (because thefind string no longer occurs) you never use that rule again, and (3) case is significant.

For example, suppose we start with the line

banana boat

and apply these rules. The sequence of transformations is shown below, where occurrences of afind string are underlined and replacements are boldfaced. Note that rule 1 was used twice, then rule 2 was used once, then rule 3 was used zero times, and then rule 4 was used once.

 BeforeAfter
 banana boat babana boat
 babana boat bababa boat
 bababa boat beba boat
 beba boat behind the goat

Input

The input contains one or more test cases, followed by a line containing only 0 (zero) that signals the end of the file. Each test case begins with a line containing the number of rules, which will be between 1 and 10. Each rule is specified by a pair of lines, where the first line is the find string and the second line is the replace-by string. Following all the rules is a line containing the text to edit. 

Output

For each test case, output a line containing the final edited text.

Both find and replace-by strings will be at most 80 characters long. Find strings will contain at least one character, but replace-by strings may be empty (indicated in the input file by an empty line). During the edit process the text may grow as large as 255 characters, but the final output text will be less than 80 characters long.

Sample Input

4
ban
bab
baba
be
ana
any
ba b
hind the g
banana boat
1
t
sh
toe or top
0

Sample Output

behind the goat
shoe or shop

Hint

when u read a line of string after an int, u can use following codes, like

scanf("%d",&t);getchar();gets(str);

or u may read some unexpected words


先说比赛时的两份代码,开始没用stl,果断程序复杂不易调试,还有一个问题是

我比赛时用了结构体来存,真是不明智,直接用两个数组来存岂不是更加简单

高效容易,易懂???

//@auther yangZongJun
/********************************************//**
Date  : 2014/03/08
题目来源:
题    意:
解题思路:
 ***********************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>

using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
const int MAXN = 11;
const int INF = 2100000000;

struct stringStruct
{
    string oldString, newString;
}stringstruct[MAXN];

int n;
string str;

int main()
{
//    freopen("input.txt", "r", stdin);
    while(scanf("%d", &n) && n)
    {
        getchar();
        for(int i=0; i < n; i++)
        {
            getline(cin, stringstruct[i].oldString);
            getline(cin, stringstruct[i].newString);
            //cin >> stringstruct[i].oldString >> stringstruct[i].newString;
            //cout << stringstruct[i].oldString << "--->" << stringstruct[i].newString << endl;
        }
        getline(cin, str);
        //cout << str << endl;
        for(int numString = 0; numString < n; numString++)
        {
            int len = str.length();//需要替换的串的总长
            string subString = stringstruct[numString].oldString;
            int lenSubString = subString.length(); //每一个替换子串的长
            int k;//k用来定位
            for(int i = 0; i < len; i++)
            {
                bool flag = true;
                for(int j = 0; j < lenSubString; j++)
                {
                    if(str[i+j] != subString[j])
                    {
                        flag = false;break;
                    }
                }
                if(flag)
                {
                    string tempString;
                    int k;
                    for( k = 0; k < len; k++)
                    {
                        if(k == i) break;
                        tempString += str[k];
                    }
                    k+=lenSubString;
                    tempString += stringstruct[numString].newString;
                    for( ; k < len; k++)
                    {
                        tempString += str[k];
                    }
                    str = tempString;
                    len = str.length();//开始没加这两行导致wa了
                    i = 0;//
                }
            }
        }
        cout << str << endl;
    }
    return 0;
}

//@auther yangZongJun
/********************************************//**
Date  : 2014/03/08
题目来源:
题    意:
解题思路:
 ***********************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>

using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
const int MAXN = 11;
const int INF = 2100000000;

struct stringStruct
{
    string oldString, newString;
}stringstruct[MAXN];

int n;
string str;

int main()
{
    //freopen("input.txt", "r", stdin);
    while(scanf("%d", &n) && n)
    {
        getchar();
        for(int i=0; i < n; i++)
        {
            getline(cin, stringstruct[i].oldString);
            getline(cin, stringstruct[i].newString);
            //cin >> stringstruct[i].oldString >> stringstruct[i].newString;
            //cout << stringstruct[i].oldString << "--->" << stringstruct[i].newString << endl;
        }
        getline(cin, str);
        //cout << str << endl;
        for(int numString = 0; numString < n; numString++)
        {
            int len = str.length();//需要替换的串的总长
            string subString = stringstruct[numString].oldString;
            int lenSubString = subString.length(); //每一个替换子串的长
            int k;//k用来定位
            for(int i = 0; i < len; i++)
            {
                bool flag = true;
                for(int j = 0; j < lenSubString; j++)
                {
                    if(str[i+j] != subString[j])
                    {
                        flag = false;break;
                    }
                }
                if(flag)
                {
                    //先erase后insert,完全可以使用replace替代
                    //cout << i << "   " <<lenSubString  << "   " << str  << str.length()<<  endl;
                    str.erase(i, lenSubString);
                    //cout << "-->" << str << endl;
                    str.insert(i, stringstruct[numString].newString);
                    //cout << "-->" << str << endl;
                    len = str.length();//这两行不能少!!!
                    i = 0;
                }

            }
        }
        cout << str << endl;
    }
    return 0;
}


赛后简短代码:

//@auther yangZongJun
/********************************************//**
Date  : 2014/03/08
题目来源:
题    意:
解题思路:
 ***********************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>

using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
const int MAXN = 11;
const int INF = 2100000000;

string s, s1[MAXN], s2[MAXN];
int n;

int main()
{
    //freopen("input.txt", "r", stdin);
    while(scanf("%d", &n) && n)
    {
        getchar();
        for(int i = 0; i < n; i++)
        {
            getline(cin, s1[i]);
            getline(cin, s2[i]);
        }
        getline(cin, s);
        int len = s.length();
        for(int i = 0; i < n; i++)
        {
            while(true)
            {
                int pos = s.find(s1[i]);
                if(pos != -1)
                {
                    s.replace(pos, s1[i].length(), s2[i]);
                }
                else break;
                /*
                if(s.find(s1[i]))
                {
                    s.erase(i, i+s1[i].length());
                    s.insert(i, s2[i]);
                }
                else break;
                */
            }
        }
        cout << s << endl;
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值