結果

問題 No.205 マージして辞書順最小
ユーザー IL_mstaIL_msta
提出日時 2015-08-24 23:28:14
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,698 bytes
コンパイル時間 2,732 ms
コンパイル使用メモリ 90,524 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-09-25 16:46:28
合計ジャッジ時間 1,763 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 2 ms
4,384 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 2 ms
4,384 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 1 ms
4,384 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,384 KB
testcase_18 AC 1 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
 
#include <iostream>
#include <iomanip>
#include <sstream>
 
#include <algorithm>
#include <cmath>
 
#include <string>
//#include <array>
#include <list>
#include <queue>
#include <vector>
#include <complex>
#include <set>
#include <map>
 
/////////
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i,n) REP(i,0,n)
#define P(p) cout<<(p)<<endl;
 
#define PII pair<int,int>
/////////
typedef long long LL;
typedef long double LD;
/////////
using namespace::std;
/////////

int main(void){
    std::cin.tie(0); 
    std::ios::sync_with_stdio(false);
    std::cout << std::fixed;//
    cout << setprecision(16);//
	
	int N;
	cin>>N;
	string S[50];
	string ans = "";
	int ansCount = 0;
	int sum = 0;
	rep(i,N){
		cin>>S[i];
		sum += (int)S[i].size();
	}
	int top[50];
	rep(i,N){
		top[i] = 0;
	}
	LL bit = 0;
	LL bitPrev = ( (LL)1<<N ) - 1;
	int sa = 0;
	int min;
	int minCount;
	int temp;
	bool flag = false;
	
	while( ansCount < sum )
	{
		if( sa == 0 ){
			min = 'z' + 1;
		}
		
		minCount = 0;
		rep(i,N){
			if( top[i]+sa >= S[i].size() ){
				flag = true;
			}else if( bitPrev & (LL)1<<i) {
				temp = S[i][ top[i]+sa ];
				if( temp == min ){
					bit += ( (LL)1<<i );
					++minCount;
				}else if( temp < min ){
					min = temp;
					bit = ( (LL)1<<i );
					minCount = 1;
				}
			}
		}
		
		if( minCount == 0 && sa != 0){
			rep(i,N){
				if( bit & (LL)1<<i ){
					for(int j=0;j<sa;++j){
						ans = ans + S[i][ top[i] + j ];
					}
					top[i] += sa;
					ansCount += sa;
				}
			}
				
			sa = 0;
			bitPrev = 0;
			rep(k,N){
				if( top[k] < S[k].size() ){
					bitPrev += (LL)1<<k;
				}
			}
		}
		else if( bit == bitPrev && minCount != 1){
			if( flag ){
				flag = false;
				rep(i,N){
					if( bit & (LL)1<<i ){
						for(int j=0;j<=sa;++j){
							ans = ans + S[i][ top[i] + j ];
						}
						top[i] += sa+1;
						ansCount += sa+1;
					}
				}
				
				sa = 0;
				bitPrev = 0;
				rep(k,N){
					if( top[k] < S[k].size() ){
						bitPrev += (LL)1<<k;
					}
				}
			}else{
				++sa;
			}
		}else if( minCount == 1 ){
			flag = false;
			rep(i,N){
				if( bit & ((LL)1<<i) ){
					int count = 0;
					for(int j=0;j<=sa;++j){
						if( j == 0){
							ans = ans + S[i][ top[i] + j ];
							++count;
						}else if( S[i][ top[i] + j -1] >= S[i][ top[i] + j] ){
							ans = ans + S[i][ top[i] + j ];
							++count;
						}else{
							break;
						}
					}
					top[i] += count;
					ansCount += count;
					sa = 0;
					
					bitPrev = 0;
					rep(k,N){
						if( top[k] < S[k].size() ){
							bitPrev += (LL)1<<k;
						}
					}
					break;
				}
			}
		}else if( minCount > 1 ){
			bitPrev = bit;
			++sa;
		}
	}
	P(ans);
	return 0;
}
0