游客 Signup | Login
中文 | En

2327 - 字符串编辑

从键盘输入一个字符串(长度<=40个字符),并以字符 '.'结束。

例如:'This is a book.' 现对该字符串进行编辑,编辑功能有:

D:删除一个字符,命令的方式为:

     D a  其中a为被删除的字符

     例如:D s  表示删除字符 's' ,若字符串中有多个 's',则删除第一次出现的。

              如上例中删除的结果为: 'Thi is a book.'


<span>I</span>:插入一个字符,命令的格式为:<span></span> 

<span>&nbsp;&nbsp;&nbsp; I a1 a2&nbsp; </span>其中<span>a1</span>表示插入到指定字符前面,<span>a2</span>表示将要插入的字符。<span></span> 

&nbsp; &nbsp; 例如:<span>I s d&nbsp; </span>表示在指定字符<span>&nbsp;'s'&nbsp;</span>的前面插入字符 '<span>d'&nbsp;</span>,若原串中有多个<span>&nbsp;'s'&nbsp;</span>,则插入在最后一个字符的前面。

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;如上例中:<span><br />

                   原串:'This is a book.'

<span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span>插入后:'<span>This ids a book.'</span> 

<span>&nbsp;</span> 

<span>R</span>:替换一个字符,命令格式为:<span></span> 

&nbsp; &nbsp; &nbsp;R a1 a2&nbsp; 其中a1为被替换的字符,a2为替换的字符,若在原串中有多个a1则应全部替换。

&nbsp; &nbsp; &nbsp;例如:

原串: 'This is a book.'

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;输入命令:<span>R o e</span> 

<span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span>替换后的字符串为:<span>'This is a beek.'</span> 

<span><br />

在编辑过程中,若出现被改的字符不存在时,则给出提示信息"Not exist"。

<br />

Input

每个测试文件只包含一组测试数据,每组输入数据包含两行:

第一行,输入一个字符串,表示原串;

第二行,输入一个字符串,表示命令。


Output

对于每组输入数据,输出编辑后的字符串,如果被改的字符不存在,则输出"Not exist"(引号不输出)。


Examples

Input

This is a book.
D s

Output

Thi is a book.

Hint

This is a book.

I s d

			<div>
				<br />
			</div>


		</td>
		<td>
			This ids a book.<br />



		</td>
	</tr>
	<tr>
		<td>
			<span>This is a book.</span><br />

R o e

		</td>
		<td>
			<span>This is a beek.</span><br />
		</td>
	</tr>
	<tr>
		<td>
			<br />
		</td>
		<td>
			<br />
		</td>
	</tr>
</tbody>


Solution C

#include<stdio.h>
#include<string.h>
#define N 45
int main()
{
	char a[N],b[10];
	int i,flag=0,len,m=1,j;
	gets(a);
	gets(b);
	len=strlen(a);
	if(b[0]=='D'){
		for(i=0;a[i]!='\0';i++){
			if(a[i]==b[2]) flag=1;
		}
		if(flag==1){
			for(i=0;a[i]!='\0';i++){
				if(m==1 && a[i]==b[2]){
					m=0;
					continue;
				}
				else{
					printf("%c",a[i]);
				}
			}
		}
		else{
			printf("Not exist");
		}
		printf("\n");
	}
	else if(b[0]=='I'){
		for(i=len-1;i>=0;i--){
			if(a[i]==b[2]){
				flag=1;
				break;
			}
		}
		if(flag==1){		
			for(j=0;j<i;j++){
				printf("%c",a[j]);
			}
			printf("%c",b[4]);
			for(j=i;a[j]!='\0';j++){
				printf("%c",a[j]);
			}
		}
		else printf("Not exist");
		printf("\n");
	}
	else{
		for(i=0;a[i]!='\0';i++){
			if(a[i]==b[2]) flag=1;
		}
		if(flag==1){
			for(i=0;a[i]!='\0';i++){
				if(a[i]==b[2]){
					printf("%c",b[4]);
				}
				else{
					printf("%c",a[i]);
				}
			}
		}
		else{
			printf("Not exist");
		}
		printf("\n");
	}
} 

Solution C++

#include<stdio.h>
#include<string.h>
int main()
{
	char a[110],b[110],ch,q,p;
	int i,count,j;
		for(i=0;;i++)
		{
			scanf("%c",&a[i]);
			if(a[i]=='.')
				break;
		}
		a[i+1]='\0';
		getchar();
		scanf("%c",&ch);
		getchar();
		if(ch=='D')
			scanf("%c",&q);
		else
			scanf("%c %c",&q,&p);
		getchar();
		if(ch=='D')
		{
			count=0,j=0;
			for(i=0;i<strlen(a);i++)
			{
				if(a[i]==q&&count==0)
				{
					count++;
				}
				else
				{
					b[j++]=a[i];
				}
			}
			b[j]='\0';
			if(count==0)
				printf("Not exist\n");
			else
			    printf("%s\n",b);
		}
		if(ch=='I')
		{
			count=0;j=strlen(a);
			for(i=strlen(a)-1;i>=0;i--)
			{
				if(a[i]==q&&count==0)
				{
					count++;
					b[j--]=a[i];
					b[j--]=p;
				}
				else
				{
					b[j--]=a[i];
				}
			}
			b[strlen(a)+1]='\0';
			if(count==0)
				printf("Not exist\n");
			else
			    printf("%s\n",b);
		}
		if(ch=='R')
		{
			count=0,j=0;
			for(i=0;i<strlen(a);i++)
			{
				if(a[i]==q)
				{
					b[j++]=p;
					count++;
				}
				else
					b[j++]=a[i];
			}
			b[j]='\0';
			if(count==0)
				printf("Not exist\n");
			else
				printf("%s\n",b);
		}
	return 0;
}

Hint

This is a book.

I s d

			<div>
				<br />
			</div>


		</td>
		<td>
			This ids a book.<br />



		</td>
	</tr>
	<tr>
		<td>
			<span>This is a book.</span><br />

R o e

		</td>
		<td>
			<span>This is a beek.</span><br />
		</td>
	</tr>
	<tr>
		<td>
			<br />
		</td>
		<td>
			<br />
		</td>
	</tr>
</tbody>


Time Limit 1 second
Memory Limit 125 MB
Discuss Stats
上一题 下一题