题目描述
Given a pattern and a string str, find if str follows the same pattern.
Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.
Examples:
pattern = "abba", str = "dog cat cat dog" should return true.
pattern = "abba", str = "dog cat cat fish" should return false.
pattern = "aaaa", str = "dog cat cat dog" should return false.
pattern = "abba", str = "dog dog dog dog" should return false.
输入
第一行一个字符串
第二行n和字符串(n表示第一行的字符串的长度)
输出true 或者 false样例输入
abba
dog cat cat dog
样例输出
true
提示
You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space.
题解:
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char a[100];
cin>>a;
int len,t=0,z=0;
len=strlen(a);
z=((float)1/(float)2)*len*(len-1);
string b[100];
for(int i=0;i<len;i++)
{
cin>>b[i];
}
for(int i=0;i<len;i++)
{
for(int j=i+1;j<len;j++)
{
if((a[i]==a[j]&&b[i]==b[j])||(a[i]!=a[j]&&b[i]!=b[j]))
t++;
}
}
if(t==z)
cout<<"true";
else
cout<<"flase";
return 0;
}
PS:我觉得这个方法好像哪里有问题。还希望各位朋友能不吝赐教!