南阳理工ACM(字符串匹配)

相关字符串问题的链接(点击打开链接)描述

上一集我们讲到 作为工作室老大的刚哥遇到很多女生的追求,你帮他个挑选了个英语成绩不错的对象。在你的帮助下,刚哥找到了个    英语学霸村    的小花,刚哥对小花的追求并不是那么一帆风顺。

事情是这样的:为了追求小花,刚哥打算给小花写点情书,然而小花却要求刚哥用英文给她写情书,并且要求刚哥不许使用百度翻译,这可难为刚哥了,刚哥自幼就爱国,对西洋文不怎么感冒,幸得健爷的帮助,刚哥成功把中文的情书翻译成了英文的情书,然而问题来了,刚哥写的情书太肉麻,健爷决定把   miss  love  kiss  这三个单词替换成  apple  banana  orange  ,眼看着今晚就要约会了,没有这些肉麻的词,刚哥约会时会不自在的.

你能在今晚10点前帮刚哥把信里面出现这三个单词的地方合理地用  miss  love  kiss  替换吗?刚哥都快急哭了,你就帮帮他吧  O(∩_∩)O~

输入
多组输入

一次输入多行

情书以 thas all 结束
程序 读到文档结束。
输出
帮刚哥把信里面出现这三个单词的地方合理地用 miss love kiss 替换, 原格式输出。
样例输入
Dear Mine: Just for one reason, I banana you so much. Nothing is impossible to a willing mind, banana included. Therefore, day after day, I wonder why, I wonder how, I wonder where you are. Time to go, I want to tell you how much I feel, and how much I banana you. When I think of you, the miles between us disappear. Seeing you will cause me an indescribable thrill, even at the sight of your handwriting will make me tremble. And the wonderful times we shared together shall always remain in my heart. You are my little angel. Just having you close fills me with banana and hope; nothing is impossible by your side. It is only when I nearly lose you that I become fully conscious of how much I value you. Accordingly, I would say, "I banana you" for millions and billions of times, and times and times again. Everything comes and goes, but banana stays. When you need someone, remember that I'd be there. If I were in heaven, I'd write your name on every star for all to see just how much you mean to me. No matter how long the road may be in the future, please cherish every moment we shared together. No matter how many years will pass away, please treasure our banana till the last day. banana is the triumph of imagination over intelligence.thas alli apple youi banana youi orange youthas all
样例输出
Dear Mine: Just for one reason, I love you so much. Nothing is impossible to a willing mind, love included. Therefore, day after day, I wonder why, I wonder how, I wonder where you are. Time to go, I want to tell you how much I feel, and how much I love you. When I think of you, the miles between us disappear. Seeing you will cause me an indescribable thrill, even at the sight of your handwriting will make me tremble. And the wonderful times we shared together shall always remain in my heart. You are my little angel. Just having you close fills me with love and hope; nothing is impossible by your side. It is only when I nearly lose you that I become fully conscious of how much I value you. Accordingly, I would say, "I love you" for millions and billions of times, and times and times again. Everything comes and goes, but love stays. When you need someone, remember that I'd be there. If I were in heaven, I'd write your name on every star for all to see just how much you mean to me. No matter how long the road may be in the future, please cherish every moment we shared together. No matter how many years will pass away, please treasure our love till the last day. love is the triumph of imagination over intelligence.thas alli miss youi love youi kiss youthas all

网站AC代码:

/*
  程序大体思路:
  char A[][7]={"apple","banana","orange"};  //A->B
  char B[][5]={"miss","love","kiss"};
  int  S[]={5,6,6};
  A[i]如果在字符串中匹配成功 需要替换成 B[i]
  输入了字符串C[] 后
  用 "apple","banana","orange" 分别与C[]匹配
  A[i]匹配成功,则对应的替换成 B[i]
  为了节省时间 我们不进行替换 而是得到替换的位置时输出"miss","love"或"kiss"
  然后i跳过"apple","banana"或"orange"的长度 即i+=S[i]
  那我们还需要在A[i]匹配成功时 再另外保存一个i 先举个例子
  比如 C="apple orange banana you"
    则 A[0]="apple"与C匹配后得到匹配的数组下标0    另外记个 0
       A[1]="banana"与C匹配后得到匹配的数组下标13  另外记个 1
       A[2]="orange"与C匹配后得到匹配的数组下标6   另外记个 2
       得到D[2][3]={{0,0},{13,1},{6,2}}
       以D[0]为主顺序 升序排列  得到 D[2][3]={{0,0},{6,2},{13,1}}
       然后在输出C的过程中 遇到 0 6 13 我们就知道对应输出 B[0],B[2],B[1].
       上句话就是:        遇到 D[0][0] D[0][1] D[0][2]我们就知道对应输出 B[D[1][0]],B[D[1][1]],B[D[1][2]]
      即当我们得到D数组并升序(D数组中存储的匹配的总个数为num):
      for(i=k=0;C[i];i++){   //输出C字符串
        if(i==D[0][k]&&k         {
            printf("%s",B[D[1][k]]);//输出B[D[1][k]]
            i+=S[D[1][k++]];//i加上 "apple","banana"或者"orange"的长度 并且D数组移动到下一个位置
        }
        else printf("%c",C[i]);//如果没有到达D的位置 原样输出
    }

*/
# include 
# define N 201
char A[][7]={"apple","banana","orange"};  //A->B
char B[][5]={"miss","love","kiss"};
int S[]={4,5,5},num,D[2][N];
char C[N];
int BF(char a[],char b[],int c[]);//返回b字符串在a中的个数 c存储b在a中的下标 从c[num]开始存储 num为全局变量
void change(int *a,int *b);//交换函数
void Qsort(int A[][N],int left,int right);//快速排序 升序
int main()
{
    int i,j,k;
    //freopen("AAA.txt","r",stdin);
    while(gets(C))
    {
    for(i=j=num=0;i<3;i++)//用 A[i]匹配C 返回匹配的个数
    {
      k=BF(C,A[i],D[0]);//k记录A[i]匹配的个数
    while(k--)D[1][j++]=i;//D[1]用来存储i
    }
    Qsort(D,0,num-1);//以A[0]升序 从下标0---  num-1  一共num个
    for(i=j=0;C[i];i++){//输出
        if(i==D[0][j]&&j=right)  return;
    while(i!=j)
    {
        while(A[0][j]>=temp && i


语言 内存 时间 结果
C 240 4 Acepped

自己写的Java代码:


package 字符串替换;


import java.util.Scanner;


public class Main {                                    //这里的类名必须为Main
         public static void main(String[] args){
                 Scanner in=new Scanner(System.in);
                  String str=in.nextLine();
                  String str_1=str.replaceAll("apple", "miss");
                 //System.out.printf(str_1);
                  String str_2=str_1.replaceAll("banana", "lova");
		//System.out.println(str_2);
		String str_3=str_2.replaceAll("orange", "kiss");
		System.out.println(str_3);
	}
}


你可能感兴趣的:(南阳理工ACM(字符串匹配))