D e s c r i p t i o n Description Description
给出字符串S和字符串T,交换s和t的某两个字符,使得交换之后的s至少要有三个连续且相同的字符,交换之后的T也一样。问有多少种不同的交换方式。
I n p u t Input Input
第一行,一个字符串S。S只含有‘R’、‘G’、‘B’三种字符,长度不超过50,不小于3。而且S任意两个相邻的字符都不相同。
第二行,一个字符串T。T只含有‘R’、‘G’、‘B’三种字符,长度不超过50,不小于3。而且T任意两个相邻的字符都不相同。
O u t p u t Output Output
一个整数,满足要求的不同交换方式的数量。
S a m p l e Sample Sample I n p u t Input Input 1 1 1
RGBRBR
BRBGRG
S a m p l e Sample Sample O u t p u t Output Output 1 1 1
1
S a m p l e Sample Sample I n p u t Input Input 2 2 2
RGRGR
GRG
S a m p l e Sample Sample O u t p u t Output Output 2 2 2
2
S a m p l e Sample Sample I n p u t Input Input 3 3 3
BGBGBGBG
RBRBRBRBRB
S a m p l e Sample Sample O u t p u t Output Output 3 3 3
0
S a m p l e Sample Sample e x p l a n a t i o n explanation explanation
O n e One One
用S的第5个字符和T的第2个字符交换。
交换后S是“RGBRRR”,T是“BBBGRG”
T w o Two Two
用S的第2个字符和T的第2个字符交换。
或者用S的第4个字符和T的第2个字符交换。
T r a i n Train Train o f of of T h o u g h t Thought Thought
直接找第一条字符串有没有"a?a"这种格式,两个相同的夹着一个不一样的
找到后再找第二条字符串的同样格式,判断交换后是否可行
C o d e Code Code
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
using namespace std;
string s,s2;
int ans;
int main()
{
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
cin>>s;
cin>>s2;
for (int i=0; i<s.size(); ++i)
{
if (s[i]==s[i+2]) {//看是否有这种格式的
for (int j=0; j<s2.size(); ++j)
{
if (s2[j]==s2[j+2]) { //同上
if (s[i+1]==s2[j] && s[i+1]==s2[j+2] && s2[j+1]==s[i] && s2[j+1]==s[i+2])//判断是否可行
ans++;
}
}
}
}
printf("%d",ans);
return 0;
}