D e s c r i p t i o n Description Description
给定两个字符串
交换两个字符串的两个字符
是否存在两个字符串都有连续的三个字符
求有多少种交换方式
I n p u t Input Input
两行,每行一个字符串
保证由“G”、“B”、“R”构成
O u t p u t Output Output
一行,为方案总数
S a m p l e I n p u t Sample Input SampleInput
RGBRBR
BRBGRG
S a m p l e O u t p u t Sample Output SampleOutput
1
思路
暴力模拟
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int Ans,len1,len2;
string s1,s2;
bool check1(int x,int y)
{
if(s2[y]==s1[x-1] && s1[x-1]==s1[x-2])return 1;
else if(s2[y]==s1[x-1] && s1[x-1]==s1[x+1])return 1;
else if(s2[y]==s1[x+1] && s1[x+1]==s1[x+2])return 1;
return 0;
}
bool check2(int x,int y)
{
if(s1[x]==s2[y-1] && s2[y-1]==s2[y-2])return 1;
else if(s1[x]==s2[y-1] && s2[y-1]==s2[y+1])return 1;
else if(s1[x]==s2[y+1] && s2[y+1]==s2[y+2])return 1;
return 0;
}
int main()
{
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
getline(cin,s1);
getline(cin,s2);
len1=s1.length();s1=" "+s1+" ";
len2=s2.length();s2=" "+s2+" ";//防止出界
for(int i=2;i<=len1+1;++i)
for(int j=1;j<=len2;++j)//枚举两个点
if(check1(i,j) && check2(i,j))Ans++;
printf("%d",Ans);
fclose(stdin);
fclose(stdout);
return 0;
}