(C++字串大小寫轉換)相似的句子

kewlgrl發表於2018-02-06

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 組資料,1K5

接下來是 2K 行。每組資料有兩行。每行以一個整數開頭,表示這個句子的單詞數量 n,然後是一個空格,緊接著有 n 個單詞用一個空格隔開。輸入保證單詞中只出現大小寫英文字母,且單詞長度不超過 20

對於 20% 的資料,有 1n2

對於 40% 的資料,單詞中只出現小寫英文字母。

對於 60% 的資料,有 1n1000

對於 100% 的資料,有 1n105

Output

對於每組資料,輸出 Case i: YES/NO。其中 i 為資料點編號(從 1 開始)。

Examples

input
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
output
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;
}


相關文章