1. 程式人生 > >判斷兩個字符集是否相同

判斷兩個字符集是否相同

一個 要求 using 文件 gin clu include i++ while

【問題描述】

從標準輸入中讀入兩個字符集(不包含空格、制表符、回車換行等空白字符),字符集中的字符無序,且可能有重復字符。當兩個字符集中字符完全相同(字符相同,字符若重復,重復個數也相同,順序不一定相同),則兩個字符集相同。編寫一程序判斷輸入的兩字符集是否相同:用1表示相同,用0表示不同。

【輸入形式】

分別在兩行上輸入兩個字符集(每個字符集中的字符個數不超過20,且第二個字符集輸入結束後也有回車換行)。

【輸出形式】

若兩字符集相同,則輸出1,否則輸出0,然後按照從小到大的順序分行輸出第一個字符集中的字符及重復個數(以一個空格分隔)。

【樣例輸入1】

helloworld9
worldhello9

【樣例輸出1】

1
9 1
d 1
e 1
h 1
l 3
o 2
r 1
w 1

【樣例輸入2】

helloworld
heloworld

【樣例輸出2】

0
d 1
e 1
h 1
l 3
o 2
r 1
w 1

【樣例說明】

樣例1中輸入的兩個字符集的字符和各個字符的重復個數都完全相同,所以輸出1,然後按從小到大的順序輸出第一個字符集中各字符及重復個數(即:有1個9,1個d,1個e,1個h,3個l,2個o,1個r,1個w)。
樣例2中輸入的兩個字符集中的字符相同,但第一個字符集中有3個l,而第二個數據集中有2個l,所以兩個字符集不同,輸出0,並按從小到大的順序輸出第一個字符集中的各字符及重復個數。

【評分標準】

該題要求判斷兩字符集是否相同,共有5個測試點。上傳C語言文件名為example2b.c。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <stdlib.h>
#include <cstdlib>
#include <string.h>
#include <string>
#include 
<cmath> #include <map> #include <time.h> using namespace std; map<char, int> mymap1; map<char, int> mymap2; int main() { char a[100]; char b[100]; cin >> a; cin >> b; for (int i = 0; a[i] != \0; i++) { mymap1[a[i]]++; } for (int i = 0; b[i] != \0; i++) { mymap2[b[i]]++; } if (strlen(a) != strlen(b) || mymap1.size()!=mymap2.size()) { cout << "0\n"; } else { map<char, int>::iterator it = mymap1.begin(); map<char, int>::iterator itt = mymap2.begin(); int flag = 1; while (it != mymap1.end() || itt != mymap2.end()) { if (it->first != itt->first || it->second != itt->second) { flag = 0; break; } it++; itt++; } cout << flag <<endl; } map<char, int>::iterator it = mymap1.begin(); while (it != mymap1.end()) { cout << it->first << " " << it->second << endl; it++; } return 0; }

判斷兩個字符集是否相同