一道字符串替换的题目。

题意:给你2*n组字符串,一个是规则,一个是替换的结果。

字符串的题目,确实麻烦,有些细节不处理好就是wa。

这里我提供1组数据

intput

1

abcdef

a

abcdef

1

ab

ababab

output

a

<empty line>

 #include <stdio.h>
#include <string.h>
#include <iostream> using namespace std; int next[100][300];
char rule[100][100],rep[100][300],ans[300],tmp[300]; void makenext(int i) //kmp求next数组。
{
int q,k;
int m = strlen(rule[i]);
next[i][0] = 0;
for (q = 1,k = 0; q < m; ++q)
{
while(k > 0 && rule[i][q] != rule[i][k])
k = next[i][k-1];
if (rule[i][q] == rule[i][k])
{
k++;
}
next[i][q] = k;
}
} void clea()
{
memset(rule,0,sizeof(rule[0]));
memset(rep,0,sizeof(rep[0]));
memset(next,0,sizeof(next[0]));
memset(tmp,0,sizeof(tmp));
memset(ans,0,sizeof(ans));
} int main()
{
int n;
while(scanf("%d",&n),n!=0)
{
clea();
getchar();
for(int i=0;i<n;i++)
{
gets(rule[i]);
gets(rep[i]);
makenext(i);
}
gets(ans);
for(int i=0;i<n;i++)
{
int len=strlen(ans),len2=strlen(rep[i]),len3=strlen(rule[i]),j=0,q=0;
for(j=0;j<len;j++)
{
while(q>0&&ans[j]!=rule[i][q])
q=next[i][q-1];
if(ans[j]==rule[i][q])
q++;
if(len3==q)
{
memset(tmp,0,sizeof(tmp));
for(int m=0;m<=len-len3+len2;m++) //替换。这里处理相对麻烦,尤其是等号的问题。
{
if(m<=j-q) tmp[m]=ans[m];
else if(m<=j-q+len2){
for(int p=0;p<len2;p++,m++)
tmp[m]=rep[i][p];
m--;}
else tmp[m]=ans[++j];
}
strcpy(ans,tmp);
j=-1;
q=0;
len=strlen(ans);
}
}
}
printf("%s\n",ans);
}
return 0;
}

poj 1572的更多相关文章

  1. POJ 1572 Automatic Editing 字符串替换,replace就够了

    题意不难理解,但是一开始还是没有看清楚题目.Replace the first occurrence of the find string within the text by the replace ...

  2. POJ题目排序的Java程序

    POJ 排序的思想就是根据选取范围的题目的totalSubmittedNumber和totalAcceptedNumber计算一个avgAcceptRate. 每一道题都有一个value,value ...

  3. POJ 题目分类(转载)

    Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...

  4. (转)POJ题目分类

    初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推. ...

  5. poj分类

    初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      ( ...

  6. poj 题目分类(1)

    poj 题目分类 按照ac的代码长度分类(主要参考最短代码和自己写的代码) 短代码:0.01K--0.50K:中短代码:0.51K--1.00K:中等代码量:1.01K--2.00K:长代码:2.01 ...

  7. POJ题目分类(按初级\中级\高级等分类,有助于大家根据个人情况学习)

    本文来自:http://www.cppblog.com/snowshine09/archive/2011/08/02/152272.spx 多版本的POJ分类 流传最广的一种分类: 初期: 一.基本算 ...

  8. POJ题目分类(转)

    初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推. ...

  9. POJ题目细究

    acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP:  1011   NTA                 简单题  1013   Great Equipment     简单题  102 ...

随机推荐

  1. [webgrid] – Ajax – (Reloading a Razor WebGrid after Ajax calls using a partial view)

    Reloading a Razor WebGrid after Ajax calls using a partial view If you are using Razor and MVC you p ...

  2. mysql Table 'performance_schema.session_variables' doesn't exist

    CMD  进入MYSQL的安装目录..Bin 下 执行 mysql_upgrade -u root -p --force 输入密码..然后等待一会儿..会跑一些东西..然后重启服务

  3. AngularJS常用指令用法详解

    ng-class 1>ng-init   ng-bind 11111 2>ng-class 111 3>ng-repeat 3.1-数据绑定     ng-repeat可以绑定数组和 ...

  4. Myeclipse中的web项目审查(jquery-2.1.1.min.js)出现错误

    前言,本来在把web项目搞得好看一些,从网上下载了一个很炫酷的模板导入web中,无奈出现了错误,如下:

  5. [译]ES6箭头函数和它的作用域

    原文来自我的前端博客: http://www.hacke2.cn/arrow-functions-and-their-scope/ 在ES6很多很棒的新特性中, 箭头函数 (或者大箭头函数)就是其中值 ...

  6. Java五道输出易错题解析(避免小错误)

    收集了几个易错的或好玩的Java输出题,分享给大家,以后在编程学习中稍微注意下就OK了. 1. 看不见的空格? 下面的输出会正常吗? package basic; public class Integ ...

  7. Web 前端

    全栈的定义是什么? 如果 前端开发/后端开发/部署/运维 都能hold住就算full stack, 我现在都overflow stack了, 需求/架构/开发/项目管理/运维 都做.  单开发这块, ...

  8. nginx TCP 代理& windows傻瓜式安装

    一.下载nginx Windows http://nginx.org/en/download.html 二.解压到目录 三.进入目录并start nginx.exe即可启动 cd d:/java/ng ...

  9. 2015年12月13日 spring初级知识讲解(四)面向切面的Spring

    2015年12月13日 具体内容待补充...

  10. [译]git checkout

    git checkout git checkout提供3种不同的功能: checking out文件, checking out commits, checking out branch. check ...