题目:题目链接
题意:
根据关键字进行字符串拷贝
把源字符串拷贝到目的字符串,如果指定关键字,则以该关键字结束(不包括关键字本身),如果拷贝失败,则得到空串。
具体要求:实现如下函数原型SafeStrcpy2KeyWord(),并在代码中调用该函数实现上述功能。该函数的实现要考虑各种可能的参数取值,以确保程序不出现崩溃。
int SafeStrcpy2KeyWord(char* pDestBuffer,//拷贝的目的地地址char* pSourceString, //拷贝的源地址int nDestBufferSize,//拷贝的目的地缓冲区长度char* szKeyWord);//指定关键字符串返回值:所拷贝的字符串长度。如果拷贝失败,则返回0。
Input
输入包含多组数据,以EOF结束
每组数据第一行为不含空格的源字符串,长度小于256;接下来的一行或多行都是关键字串(长度小于16),一直到END结束。“NULL”表示关键字串为空,此时输出的拷贝后的长度应为0,拷贝后的字符串为空串(也用”NULL”表示,见下文)。
Output
对于每组数据输出拷贝的长度和拷贝后的目的字符串,以空格分隔。如果该目的字符串为空,则用”NULL”表示。
Sample Input
/home/tony/work_server/1/rtest/relayer.out
/
/t
/1/r
.
NULL
END
Sample Output
0 NULL
5 /home
22 /home/tony/work_server
38 /home/tony/work_server/1/rtest/relayer
0 NULL
就是找到关键字以前的字符输出就可以了,开始以为是每次是同时考虑这么多的关键字,觉得有点麻烦。后来仔细一看,就只是考虑一个关键字。直接比较吧,怕会超时,看了一下status,超时的并不多,就试了一发,WA了,唉,粗心了,在判断order == null 的时候忘记continue了,好吧加上,A了,就只是比价而已:
#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <map>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <queue>
#include <set>
#include <stack>
#include <functional>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cassert>
#include <bitset>
#include <stack>
#include <ctime>
#include <list>
using namespace std;
#define N 1010
char mother[N];
string order;
int main()
{
memset(mother, 0, sizeof(mother));
while(scanf("%s", mother) != EOF)
{
while(cin >> order)
{
if(order == "END")
break;
if(order == "NULL")
{
cout << 0 << ' ' << "NULL"<<endl;
continue;
}
int cnt = 0;
string ans = "";
int lenm = strlen(mother);
int lenz = order.length();
for(int i = 0; i < lenm; ++i)
{
if(mother[i] != order[0])
{
ans += mother[i];
cnt++;
}
else
{
int tt = i;
int j;
int fp = 0;
for(j = 0; j < lenz; ++j)
{
if(mother[tt] != order[j])
{
fp = 1;
break;
}
else
tt++;
}
if(fp == 1)
{
ans += mother[i];
cnt++;
}
else
{
i = lenm;
break;
}
}
}
if(cnt == 0)
cout << 0 << ' ' << "NULL" << endl;
else cout << cnt <<' ' << ans << endl;
}
memset(mother, 0, sizeof(mother));
}
return 0;
}
努力努力...