同构词
Time Limit:1000MS Memory Limit:65536K
Total Submit:97 Accepted:25
Description
两个字符串由相同的字母组成(字母的个数可不等),则为同构词。如"ab"和"ababbb" "abbbc"和"abcc"
Input
第一行有一个整数N表示N组测试数据。
接下来有N行,且每行都有两个字符串(均为小写字母组成且长度小于10000)。
Output
输出N行。若两字符串为同构词输出"Yes",否则输出"No"。
Sample Input
2
ab ababbb
abc aac
Sample Output
Yes
No
Hint
ahstu@icpc2014
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char const *argv[])
{
int n;
cin>>n;
while(n--)
{
string a,b;
cin>>a>>b;
int p[26]={0},q[26]={0};
for(int i=0; i<a.size(); i++)
{
p[a[i]-'a']++;
}
for(int i=0; i<b.size(); i++)
{
q[b[i]-'a']++;
}
bool flag=true;
for(int i=0; i<26; i++)
{
if((p[i]==0&&q[i]!=0)||(p[i]!=0&&q[i]==0))flag=false;
}
if(flag)cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return 0;
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
/// 对每一个字符串都生成一个HashCode
int hash(char *s)
{
int result=0;
for(; *s; s++) {
result|=(1<<(*s-'a'));
}
return result;
}
int main(int argc,char *argv[])
{
int Test;
char s[100000+10];
char t[100000+10];
scanf("%d",&Test);
while(Test--) {
scanf("%s%s",s,t);
printf("%s\n",hash(s)==hash(t)?"Yes":"No");
}
return 0;
}