[ZCMU OJ]1425: Careless Tony(小坑)

Description

Tony is such a careless typist that he finds himself making mistakes AGAIN. What's worse, the cursor key is not working so that he can only use the backspace key to reach the place where the mistake is, and then type whatever he's deleted on the way AGAIN :(

Now let's help Tony find out at least how long it will cost him to correct his mistake.

Input

The first line of input contains an integer N, which is the number of test cases. Then N test cases follow.

Each test case consists of 3 lines of input:

the 1st line contains a positive integer t (<= 100), which is the time taken for Tony to delete/input a character;

the 2nd line contains the correct content of text;

and the 3rd line contains the text typed by Tony.

Note: The text contents contain only the readable characters. The total length of each text is no longer than 80 characters.

Output

For each test case, print in one line the minimal time taken for Tony to correct his mistake.

Sample Input

2 1
WishingBone
WashingBone
1
Oops
Oooops

Sample Output

20
6

———————————————————————————————————————————

题意:给定测试组数、删除/增加一个字符串所需要的时间、正确字符串与错误字符串,输出修改该错误字符串的时间。要修改目标字符串必须从右到左删除直到目标字符串,再重新打印剩余部分的字母。

思路:首先建立两个数组存储正确与错误的输入,并利用pos标记错误的位置(pos初始化为错误数组的最后一个位置,因为要考虑没有写错的情况);再将两个数组平行比较找到错误字符出现的位置。最后用错误数组的长度减去pos的差乘以修改时间以及正确数组的长度减去pos的差乘以修改时间即可算出修改与输入的时间之和。

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int T;
	int t;
	char cor[100],mis[100];
	
	cin>>T;
	while(T--)
	{
		memset(cor,'/0',sizeof(cor));
		memset(mis,'/0',sizeof(mis)); //字符数组初始化 
		cin>>t;
		cin>>cor;
		cin>>mis;
		
		int pos=strlen(mis);//考虑没有写错的情况 
		
		for(int i=0;i<strlen(mis);i++)
		{
			if(cor[i]!=mis[i]&&mis[i]!='\0')
			{
				pos=i;//找到错误位置立即跳出 
				break; 
			}
		}
		int time=(strlen(mis)-pos)*t+(strlen(cor)-pos)*t;
		          //删除错误的      输入正确的(别忘了乘删除和输入所需时间) 
		cout<<time<<endl;
	}
}

猜你喜欢

转载自blog.csdn.net/Solar_Zheng0817/article/details/124553249