我是個對電腦科學有興趣的學生,我會貼上我的學習歷程及生活心情,也請大大們多多指教。 :)

2015年3月20日 星期五

[UVa] 195 - Anagram

題目網址: http://goo.gl/MYjGwI

題 意: 給一個字串,輸出這個字串所有的排序,輸出的排序要以 "字母順序" 輸出,(字母順序: AaBbCc......Zz)。


解法: 擅用STL。

TAG: STL

注意:

程式碼:
/**
* Tittle: 195 - Anagram
* Author: Cheng-Shih, Wong
* Date: 2015/03/20
*/
// include files
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <string>
using namespace std;
// definitions
#define FOR(i,a,b) for( int i=(a),_n=(b); i<=_n; ++i )
#define clr(x,v) memset( x, v, sizeof(x) )
// declarations
int n;
string s;
// functions
bool cmp( char A, char B )
{
bool isua = isupper( A );
bool isub = isupper( B );
if( isua ) A = A-'A'+'a';
if( isub ) B = B-'A'+'a';
if( (isua^isub) && A==B ) return isua;
return A<B;
}
// main function
int main( void )
{
// input
scanf( "%d", &n );
while( n-- ) {
cin >> s;
// solve & output
sort( s.begin(), s.end(), cmp );
do {
cout << s << endl;
} while( next_permutation( s.begin(), s.end(), cmp ) );
}
return 0;
}

沒有留言:

張貼留言

任何意見都樂意傾聽