結果
| 問題 |
No.205 マージして辞書順最小
|
| コンテスト | |
| ユーザー |
IL_msta
|
| 提出日時 | 2015-08-24 23:28:14 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,698 bytes |
| コンパイル時間 | 701 ms |
| コンパイル使用メモリ | 90,512 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-18 13:11:55 |
| 合計ジャッジ時間 | 1,309 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 4 WA * 11 |
ソースコード
#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;
}
IL_msta