結果
| 問題 |
No.205 マージして辞書順最小
|
| コンテスト | |
| ユーザー |
IL_msta
|
| 提出日時 | 2015-08-25 01:18:42 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,833 bytes |
| コンパイル時間 | 679 ms |
| コンパイル使用メモリ | 90,288 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-18 13:15:49 |
| 合計ジャッジ時間 | 1,203 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 9 WA * 6 |
ソースコード
#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;
int minCount;
int temp;
bool flag = false;
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] ] >= S[i][ top[i] + j] ||
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] ] >= S[i][ top[i] + j] ||
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;
}
}
P(ans);
return 0;
}
IL_msta