Enigma
题目描述
Given an initial configuration, the World War II German encryption machine Enigma replaces each letter typed on the keyboard with some other letter. The replacement strategy was quite complex, but the machine had a vulnerability: a letter would never be replaced by itself! This vulnerability was exploited by Alan Turing, who worked in the cryptanalysis of Enigma during the war. His goal was to find the initial configuration of the machine using the assumption that the message contained a certain usual expression of communication, such as the word ARMADA, for example. These expressions were called cribs. If the message FDMLCRDMRALF was encrypted, for example, the process of testing the possible configurations of the machine would be simpler because the word ARMADA, if present in the encrypted message, could only be in two positions, illustrated in the table below with an arrow. The remaining five positions could not match the crib ARMADA because at least one letter in the crib, underlined in the table below, would match its correspondent in the encrypted message; as the machine would never replace a letter by itself, these five positions could be ignored in the tests.
In this problem your program should compute, given a ciphertext and a crib, the number of possible positions for the crib in the encrypted message.
输入
The first line of the input contains the encrypted message, which is a sequence of at least one letter and a maximum of 104 letters. The second line of the input contains the crib, which is a sequence of at least one letter and at most the same number of letters of the message. Only the 26 uppercase English letters appear on message and in the crib.
输出
Print a line containing an integer, indicating the number of possible starting positions for the crib in the encrypted message.
样例输入
FDMLCRDMRALF
ARMADA
样例输出
2
题意:
对应字符串匹配,每次向后移动一位,如果对应单个字符结果都不相同,结果加一。
思路:
直接模拟
代码1:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1,s2;
cin>>s1>>s2;
int res=0;
for(int i=0;i<s1.size()-s2.size()+1;i++)
{
int k=i;
for(int j=0;j<s2.size();j++)
{
if(s1[k++]==s2[j])
{
res++;
break;
}
}
}
cout<<s1.size()-s2.size()-res+1;
}
代码2:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
using namespace std;
int main()
{
string ch1,ch2;
int ans=0;
cin>>ch1>>ch2;
for(int i=0;i<=(ch1.size()-ch2.size());i++)
{
int k=i,flag=0;
for(int j=0;j<ch2.size();j++)
if(ch1[k++]==ch2[j])
{
flag=1;
break;
}
if(flag==0)
ans++;
}
cout<<ans<<endl;
return 0;
}