C - String Transformation
Time Limit: 2 sec / Memory Limit: 1024 MB
Score : 300300 points
Problem Statement
You are given strings SS and TT consisting of lowercase English letters.
You can perform the following operation on SS any number of times:
Operation: Choose two distinct lowercase English letters c1c1 and c2c2, then replace every occurrence of c1c1 with c2c2, and every occurrence of c2c2 with c1c1.
Determine if SS and TT can be made equal by performing the operation zero or more times.
Constraints
- 1≤|S|≤2×1051≤|S|≤2×105
- |S|=|T||S|=|T|
- SS and TT consists of lowercase English letters.
Input
Input is given from Standard Input in the following format:
SS TT
Output
If SS and TT can be made equal, print Yes
; otherwise, print No
.
Sample Input 1 Copy
Copy
azzel apple
Sample Output 1 Copy
Copy
Yes
azzel
can be changed to apple
, as follows:
- Choose
e
as c1c1 andl
as c2c2.azzel
becomesazzle
. - Choose
z
as c1c1 andp
as c2c2.azzle
becomesapple
.
Sample Input 2 Copy
Copy
chokudai redcoder
Sample Output 2 Copy
Copy
No
No sequences of operation can change chokudai
to redcoder
.
Sample Input 3 Copy
Copy
abcdefghijklmnopqrstuvwxyz ibyhqfrekavclxjstdwgpzmonu
Sample Output 3 Copy
Copy
Yes
题意:给两个字符串,可以进行一个操作:第一个字符串选一个字符,第二个字符串选一个字符,然后第一个字符串中的所有这个字符都会被替换成第二个字符串的这个字符,问能否使第一个字符串变成第二个字符串
想要变成第二个字符串,字符间必须是一一对应的关系
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+7;
int num[maxn];
int main()
{
string stra;
string strb;
cin>>stra>>strb;
int len=stra.size();
memset(num,-1,sizeof(num));
for(int i=0;i<len;i++)
{
if(num[stra[i]]==-1)
{
num[stra[i]]=strb[i];
}
else
{
if(num[stra[i]]!=strb[i])
{
cout<<"No"<<endl;
return 0;
}
}
}
memset(num,-1,sizeof(num));
for(int i=0;i<len;i++)
{
if(num[strb[i]]==-1)
{
num[strb[i]]=stra[i];
}
else
{
if(num[strb[i]]!=stra[i])
{
cout<<"No"<<endl;
return 0;
}
}
}
cout<<"Yes"<<endl;
return 0;
}