結果

問題 No.205 マージして辞書順最小
ユーザー IL_mstaIL_msta
提出日時 2015-08-25 01:08:12
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 2,906 bytes
コンパイル時間 711 ms
コンパイル使用メモリ 89,416 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-25 16:51:28
合計ジャッジ時間 1,677 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,384 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 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 = ( (LL)1<<N ) - 1;
	LL bitNext = 0;
	int sa = 0;
	int min,minPrev;
	int minCount;
	int temp;
	bool flag = false;
	minPrev = 'z' + 1;
	while( ansCount < sum )
	{
		min			= 'z' + 1;
		minCount	= 0;
		bitNext		= 0;

		rep(i,N){
			if( bit & (LL)1<<i) {
				if( top[i]+sa >= (int)S[i].size() ){
					flag = true;
					continue;
				}
				temp = S[i][ top[i]+sa ];
				if( temp == min ){
					bitNext += ( (LL)1<<i );
					++minCount;
				}else if( temp < min ){
					min = temp;
					bitNext = ( (LL)1<<i );
					minCount = 1;
				}
			}
		}
		///////////////
		if( minCount == 0 ){//グループの最小値が得られなかった
			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;
					///////
					break;
				}
			}
			
			//初期化
			sa = 0;
			bit = 0;
			rep(k,N){
				if( top[k] < (int)S[k].size() ){
					bit += (LL)1<<k;
				}
			}
		}
		else if( minCount == 1 ){//最小値が一つ
			flag = false;
			rep(i,N){
				if( bitNext & ((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;
					bit = 0;
					rep(k,N){
						if( top[k] < (int)S[k].size() ){
							bit += (LL)1<<k;
						}
					}
					break;
				}
			}
		}
		else if( minCount > 1 ){
			bit = bitNext;
			++sa;
		}

		//////////////
		if( sa ){//続行中なら保存
			minPrev = min;
		}else{//最探索で初期化
			minPrev = 'z' + 1;
		}
	}
	P(ans);
	return 0;
}
0