fc2ブログ



Unreliable Message

2007.08.23 12:49  ACM/ICPC 2003

[問題] ACM/ICPC Asia Regional 2003 Aizu: Problem A

[解説]

文字列操作。

[サンプルプログラム]


001 #include<iostream>
002 #include<string>
003 #include<algorithm>
004 
005 using namespace std;
006 
007 void shiftRight( string &line ){
008     line = line[line.size()-1] + line.substr(0, line.size()-1);
009 }
010 
011 void shiftLeft( string &line ){
012     line = line.substr(1, line.size()-1 ) + line[0];
013 }
014 
015 void swapHalf( string &line ){
016     int mid = line.size()/2;
017     if ( line.size() % 2 == 0 )
018     line = line.substr(mid, line.size()-mid) + line.substr(0, mid);
019     else
020     line = line.substr(mid + 1, mid) + line[mid] + line.substr(0, mid);
021 }
022 
023 void reverse( string &line){
024     reverse( line.begin(), line.end() );
025 }
026 
027 void decrement( string &line ){
028     for ( int i = 0; i < line.size(); i++ ){
029         if ( isalpha(line[i]) ) continue;
030         if ( line[i] == '0' ) line[i] = '9';
031         else line[i]--;
032     }
033 }
034 
035 void increment( string &line ){
036     for ( int i = 0; i < line.size(); i++ ){
037         if ( isalpha(line[i]) ) continue;
038         if ( line[i] == '9' ) line[i] = '0';
039         else line[i]++;
040     }
041 }
042 
043 
044 main(){
045     int tcase; cin >> tcase;
046     string command, line;
047     
048     for ( int i = 0; i < tcase; i++ ){
049         cin >> command >> line;
050         
051         for ( int c = command.size()-1; c >= 0; c-- ){
052             if ( command[c] == 'J' ) shiftRight( line );
053             else if ( command[c] == 'C' ) shiftLeft( line );
054             else if ( command[c] == 'E' ) swapHalf( line );
055             else if ( command[c] == 'A' ) reverse( line );
056             else if ( command[c] == 'P' ) decrement( line );
057             else if ( command[c] == 'M' ) increment( line );
058         }
059         
060         cout << line << endl;
061     }
062 }
スポンサーサイト



テーマ : プログラミング - ジャンル : コンピュータ

| コメント(0) | トラックバック(0) | ↑ページトップ |

この記事へのコメント

コメントを書く


管理人にのみ表示

↑ページトップ

この記事へのトラックバック

この記事にトラックバックする(FC2ブログユーザー)

↑ページトップ