Gym 100989E 字符串

Description

standard input/output

Islam is usually in a hurry. He often types his passwords incorrectly. He hates retyping his password several times whenever he tries to login, especially that his passwords are usually very long. He believes that websites should be tolerant with very long passwords. In other words, he believes that if a password is very long, and there is only one mistake in the password, the website should allow the user to login.

Your task is to check if an entered password should be accepted according to Islam, or not. The entered password will be accepted if it matches the user’s password, or if the user’s password length is at least 8 characters and the user made a mistake with only one character (either replaced it with a wrong character or dropped it).

Given the user’s password, and the entered password, determine if the entered password should be accepted according to Islam.

Input

The first line of input contains the user’s password.

The second line of input contains the entered password.

Both strings contain only lowercase and uppercase English letters.

The length of each string is at least 1 and at most 100.

Output

Print yes if the entered password should be accepted according to Islam, otherwise print no.

Sample Input

 
Input
AgentMahone
IslamIsMahone
Output
no
Input
ofmahone
ofmahome
Output
yes
Input
algorithms
algorthms
Output
yes
Input
Mahone
mahonE
Output
no

题意:

题解:给两个串a,b; a的长度len1 b的长度len2   a长度小于8的直接判断,长度大于等于8的 根据题意
两个串的长度只存在 len1==len2或者 len1-len2=1两种情况才可能yes,分别特判一下就可以了。
   len1==len2情况下 只能是两个串相同或者某一个字母被替换,遍历一遍验证
   len1-len2==1 情况下 只能是丢失一个字母  遍历一遍 当遇到字母不同时,b串向后移动一位继续遍历
                       当只出现一次对应字母不同时,才能说明只丢失了一个字母
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 using namespace std;
 5 char a[105];
 6 char b[105];
 7 int main()
 8 {
 9     scanf("%s",a);
10     scanf("%s",b);
11     int len1=strlen(a);
12     int len2=strlen(b);
13     if(len1<8)
14     {
15         if(strcmp(a,b)==0)
16             cout<<"yes"<<endl;
17         else
18             cout<<"no"<<endl;
19     }
20     else
21     {
22         if(len2>len1)
23         {
24             cout<<"no"<<endl;
25             return 0;
26         }
27         if(strcmp(a,b)==0)
28         {
29             cout<<"yes"<<endl;
30             return 0;
31         }
32         int flag=0;
33         if(len1==len2)
34         {
35             for(int i=0;i<len1;i++)
36             {
37                 if(a[i]!=b[i])
38                 {
39                     flag++;
40                 }
41             }
42             if(flag==1)
43             {
44                 cout<<"yes"<<endl;
45                 return 0;
46             }
47             else
48             {
49                 cout<<"no"<<endl;
50                 return 0;
51             }
52         }
53         if(len1-len2==1)
54         {
55         for(int i=0;i<len1;i++)
56         {
57             if(a[i]!=b[i])
58             {
59                 for(int j=len2-1;j>=i;j--)
60                 {
61                     b[j+1]=b[j];
62                 }
63                 b[i]=' ';
64                 flag++;
65             }
66         }
67           if(flag==1)
68             {
69                 cout<<"yes"<<endl;
70                 return 0;
71             }
72             else
73             {
74                 cout<<"no"<<endl;
75                 return 0;
76             }
77         }
78         cout<<"no"<<endl;
79     }
80     return 0;
81 }
原文地址:https://www.cnblogs.com/hsd-/p/5664079.html