24. 相似的句子
Time limit per test: 2.0 seconds
Memory limit: 256 megabytes
有两个英文句子。如果把第一个句子中的单词进行某种排列,恰好得到第二个句子,那么就称这两个句子是相似的。(注意,单词比较是不区分大小写的。)例如:Today is Friday 和 friday toDAY Is 是相似的;it is a nice day 和 A nice day it is 是相似的;但 its beautiful isnt it 和it is Beautiful its isnt 不是相似的。
现在给出两个句子,判断他们是否相似。如果相似输出 YES,不相似输出 NO。
Input
第一行一个整数 K,表示接下来有 K 组数据,1≤K≤5。
接下来是 2⋅K 行。每组数据有两行。每行以一个整数开头,表示这个句子的单词数量 n,然后是一个空格,紧接着有 n 个单词用一个空格隔开。输入保证单词中只出现大小写英文字母,且单词长度不超过 20。
对于 20% 的数据,有 1≤n≤2。
对于 40% 的数据,单词中只出现小写英文字母。
对于 60% 的数据,有 1≤n≤1000。
对于 100% 的数据,有 1≤n≤105。
Output
对于每组数据,输出 Case i: YES/NO。其中 i 为数据点编号(从 1 开始)。
Examples
4 3 Today is Friday 3 friday toDAY Is 5 it is a nice day 5 A nice day it is 4 its beautiful isnt it 5 it is Beautiful its isnt 3 its its its 3 sit its its
Case 1: YES Case 2: YES Case 3: NO Case 4: NO
由于ECNU OJ不能识别strlwr,所以还真是头疼了一下:
‘strlwr’ was not declared in this scope
strcpy(a[i].s,strlwr(str));
使用transform(str.begin(), str.end(), s.begin(), ::tolower);将字符串全部转换成小写:
string str;
string s;
transform(str.begin(), str.end(), s.begin(), ::tolower);
类似的,使用transform(str.begin(), str.end(), s.begin(), ::toupper);将字符串全部转换成大写。
#include<bits/stdc++.h>
using namespace std;
struct N
{
string s;
} a[100010],b[100010];
int cmp(N x,N y)//结构体排序
{
return x.s>y.s;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("F:/cb/read.txt","r",stdin);
//freopen("G:/x/out.txt","w",stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int t,ca=0;
cin>>t;
while(t--)
{
int n,m;
cin>>n;
for(int i=0; i<n; ++i)
{
string str;;
cin>>str;
transform(str.begin(), str.end(), str.begin(), ::tolower);
a[i].s=str;
//cout<<a[i].s<<endl;
}
cin>>m;
bool flag=true;
if(m!=n) flag=false;
else
{
for(int i=0; i<n; ++i)
{
string str;;
cin>>str;
transform(str.begin(), str.end(), str.begin(), ::tolower);
b[i].s=str;
//cout<<b[i].s<<endl;
}
sort(a,a+n,cmp);
sort(b,b+n,cmp);
for(int i=0; i<n; ++i)
{
if(a[i].s!=b[i].s)
{
flag=false;
break;
}
}
}
if(flag) cout<<"Case "<<++ca<<": YES"<<endl;
else cout<<"Case "<<++ca<<": NO"<<endl;
}
return 0;
}
本文介绍了一个算法,用于判断两个英文句子是否由相同的单词通过不同排列组成。该算法首先将句子中的所有单词转换为小写形式,然后进行排序比较,最终确定两个句子是否相似。
相似的句子&spm=1001.2101.3001.5002&articleId=79273357&d=1&t=3&u=40c9b95cd7224fd5a1261964aa2782a2)

被折叠的 条评论
为什么被折叠?



