UVA 1162 Transcript

来源:互联网 发布:无线网桥传输网络信号 编辑:程序博客网 时间:2024/06/05 21:14

Transcript

 

A company wants to hire a new employee. The selection process consists of several attention tests, one of them consists in: each candidate must type each character he sees in a flashing screen, using a given keyboard. You are asked to write down a program to score the candidates, given the original sequence of characters, and what the candidate actually typed. Scoring is based on the kind of actions the candidate may perform. For each character flashing in the screen, she may only:

1.          Correctly type the character;

2.          Omit the character;

3.          By mistake, type a different character.

The score or penalty given to each action depends on the keyboard layout considered. The keyboard is a matrix of n rows and m columns.

The distance between the characters at coordinates (i1,j1) and (i2,j2) is given by the maximum of |i1- i2| and |j1-j2|. For example, in the keyboard below (3 rows by 5 columns), the distance between the character "a", at (2,1), and the character "h" at (3,5) is 4, and the distance between the character "o" and "h" is 2. In this example, the largest distance between any two characters is 4. For any keyboard the largest distance between any two characters is conventionally referred to by TOP.

 

1

e

g

y

i

m

2

a

n

o

w

s

3

u

f

l

t

h

 

1

2

3

4

5

The score given to the correct transcription of one character is TOP+1. The penalty given for the omission of one character (action 2) is TOP+1. The score given for changing a character for another (action 3) is TOP+1 minus the distance between the two characters involved in the mistake.  For example, considering the keyboard shown above, the following action scores apply: Score for the correct transcript of one character: 5; Penalty for the omission of one character: 5; Score for changing character "o" to character "h": 5-2=3.

A scoring of a test is the sum of the scores given to each character typed minus the sum of the penalties for each character omitted. Since the scoring is only done after the test finishes, it is not possible to be sure about when specific actions were realized (e.g, did the candidate skip a character, or mistyped it?). To avoid complaints, the final score given is the highest possible value for scoring the candidate, according to the rules explained above. For example, if the text to transcribe is "time" and the candidate types "yme", we may score it in several ways:

To omit "t", change  "i" for "y" and correctly transcribe "m" and "e";

To change "t" for "y", omit "i" and correctly transcribe "m" and "e";

To change "t" for "y", change "i" for "m", omit "m" and correctly transcribe "e";

To change "t" for "y", change "i" for "m", change "m" for "e" and omit "e".

Each one of these possibilities has one score associated (9, 8, 7, and 3 points, respectively). Thus, the candidate’s final score is 9 points. Write a program that, considering the shape of a keyboard, the text to be transcribed by the candidate, and the actual transcript produced by the candidate returns the final score of the candidate.

Input

 

The input file contains several test cases, each of them as described below. 

The first line contains an integer N, stating the number of rows in the keyboard. The next N lines, all with the same length, contain a string with the sequence of characters in the corresponding keyboard row.  The keyboard will not have more than 20 rows and 30 columns. Then, the next two lines contains the text to be transcribed by the candidate, and the text typed by the candidate. These texts will be no longer than 500 characters each.

Output

 

For each test case, a single line containing an integer stating the final score of the candidate.

 

Sample Input

3

egyim

anows

uflth

time

yme

 

Sample Output

9

 

题目大意:给定一个n*m(m由输入单行字符串长度决定)的字符键盘,定义其中任意两个字符距离为max(abs(x1-x2),abs(y1-y2));现给定原串以及参与者打出的字符串,求他能获得的最大分数。

1.正确打对一个字符,获得Top分(Top=max(n,m));

2.漏打一个字符,罚Top分;

3.错打一个字符,获得(Top-两字符的距离);

分析:dp[i][j] 表示对于遍历到原串的第i个字符,匹配了打出的字符串前j个所获的最大分数

转移方程:dp[i][j]=max(dp[i-1][j]-Top,dp[i-1][j-1]+d[s1[i]][s2[j]]);

#include<cstdio>#include<cstring>const int INF=0x3f3f3f3f;int abs(int x){return x<0?-x:x;}int max(int a,int b){return a>b?a:b;}int min(int a,int b){return a<b?a:b;}int dp[502][502],d[601][601];char s[20][30],s1[502],s2[502];int main(){int n,m,k,i,j,p,q,l1,l2;while(~scanf("%d",&n)){getchar();for(i=0;i<n;i++)gets(s[i]);m=strlen(s[0]);int Top=max(n,m),tmp;for(i=0;i<n;i++)for(j=0;j<m;j++){for(p=0;p<n;p++)for(q=0;q<m;q++)d[s[i][j]][s[p][q]]=Top-max(abs(i-p),abs(j-q));}gets(s1+1);l1=strlen(s1+1);gets(s2+1);l2=strlen(s2+1);for(i=0;i<=l1;i++){dp[i][0]=-i*Top;for(j=1;j<=l2;j++)dp[i][j]=-INF;}for(i=1;i<=l1;i++)for(j=1;j<=i;j++)dp[i][j]=max(dp[i-1][j]-Top,dp[i-1][j-1]+d[s1[i]][s2[j]]);printf("%d\n",dp[l1][l2]);}return 0;}


原创粉丝点击