Submission #50773377
Source Code Expand
#include<bits/stdc++.h>
#include<set>
using namespace std;
const int N=20;
int n,f[N][1<<N],v[N],pos,len=1e9;
string a[N],b[N],g[N][1<<N],ans;
bool cmp(string a,string b)
{
int k=min(a.size(),b.size());
for(int i=0;i<k;i++)
if(a[i]<b[i])return 1;
else if(a[i]>b[i])return 0;
return a.size()<b.size();
}
int cal(string a,string b)
{
int ret=-1;
for(int i=min(a.size()-1,b.size()-1);i>=0;i--)
{
int flag=1;
for(int j=0;j<=i;j++)
if(a[a.size()-1-j]!=b[i-j])flag=0;
if(flag)
{
ret=i;
break;
}
}
return ret;
}
int main()
{
memset(f,0x3f,sizeof(f));
scanf("%d",&n);
for(int i=0;i<n;i++)
cin>>b[i];
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
{
if(b[i].find(b[j])!=-1)v[j]=1;
else if(b[j].find(b[i])!=-1)v[i]=1;
}
for(int i=0;i<n;i++)
if(!v[i])a[pos++]=b[i];
sort(a,a+pos,cmp);
for(int i=0;i<pos;i++)
f[i][1<<i]=a[i].size(),g[i][1<<i]=a[i];
//printf("%d\n",pos);
for(int j=0;j<1<<pos;j++)
for(int i=0;i<pos;i++)
if(f[i][j]<1e9)
{
for(int t=0;t<pos;t++)
if(((j>>t)&1)==0)
{
int p=cal(g[i][j],a[t]);
//cout<<g[i][j]<<' '<<a[t]<<' '<<p<<'\n';
if(f[i][j]+(a[t].size()-1-p)<f[t][j|(1<<t)])
{
f[t][j|(1<<t)]=f[i][j]+(a[t].size()-1-p);
g[t][j|(1<<t)]=g[i][j];
for(int h=p+1;h<a[t].size();h++)
g[t][j|(1<<t)]+=a[t][h];
//cout<<g[t][j|(1<<t)]<<'\n';
}
else if(f[i][j]+(a[t].size()-1-p)==f[t][j|(1<<t)])
{
string r=g[i][j];
for(int h=p+1;h<a[t].size();h++)
r+=a[t][h];
if(cmp(r,g[t][j|(1<<t)]))g[t][j|(1<<t)]=r;
}
}
}
for(int i=0;i<pos;i++)
if(len>f[i][(1<<pos)-1])len=f[i][(1<<pos)-1],ans=g[i][(1<<pos)-1];
else if(len==f[i][(1<<pos)-1])
{
if(cmp(g[i][(1<<pos)-1],ans))ans=g[i][(1<<pos)-1];
}
cout<<ans.size();
return 0;
}
Submission Info
| Submission Time | |
|---|---|
| Task | G - Compress Strings |
| User | lishujia |
| Language | C++ 20 (gcc 12.2) |
| Score | 0 |
| Code Size | 2153 Byte |
| Status | TLE |
| Exec Time | 5565 ms |
| Memory | 938916 KiB |
Compile Error
Main.cpp: In function ‘int main()’:
Main.cpp:40:35: warning: comparison of integer expressions of different signedness: ‘std::__cxx11::basic_string<char>::size_type’ {aka ‘long unsigned int’} and ‘int’ [-Wsign-compare]
40 | if(b[i].find(b[j])!=-1)v[j]=1;
| ~~~~~~~~~~~~~~~^~~~
Main.cpp:41:40: warning: comparison of integer expressions of different signedness: ‘std::__cxx11::basic_string<char>::size_type’ {aka ‘long unsigned int’} and ‘int’ [-Wsign-compare]
41 | else if(b[j].find(b[i])!=-1)v[i]=1;
| ~~~~~~~~~~~~~~~^~~~
Main.cpp:58:53: warning: comparison of integer expressions of different signedness: ‘std::__cxx11::basic_string<char>::size_type’ {aka ‘long unsigned int’} and ‘int’ [-Wsign-compare]
58 | if(f[i][j]+(a[t].size()-1-p)<f[t][j|(1<<t)])
| ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
Main.cpp:62:48: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::__cxx11::basic_string<char>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
62 | for(int h=p+1;h<a[t].size();h++)
| ~^~~~~~~~~~~~
Main.cpp:66:74: warning: comparison of integer expressions of different signedness: ‘std::__cxx11::basic_string<char>::size_type’ {aka ‘long unsigned int’} and ‘int’ [-Wsign-compare]
66 | else if(f[i][j]+(a[t].size()-1-p)==f[t][j|(1<<t)])
| ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
Main.cpp:69:64: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::__cxx11::basic_string<char>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
69 | for(int h=p+1;h<a[t].size();h++)
| ~^~~~~~~~~~~~
Main.cpp:34:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
34 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
Judge Result
| Set Name | Sample | All | ||||||
|---|---|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 0 / 600 | ||||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt |
| All | 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 02_random2_00.txt, 02_random2_01.txt, 02_random2_02.txt, 02_random2_03.txt, 02_random2_04.txt, 02_random2_05.txt, 02_random2_06.txt, 02_random2_07.txt, 02_random2_08.txt, 02_random2_09.txt, 02_random2_10.txt, 02_random2_11.txt, 02_random2_12.txt, 02_random2_13.txt, 02_random2_14.txt, 02_random2_15.txt, 02_random2_16.txt, 02_random2_17.txt, 02_random2_18.txt, 02_random2_19.txt, 02_random2_20.txt, 02_random2_21.txt, 02_random2_22.txt, 02_random2_23.txt, 03_random3_00.txt, 03_random3_01.txt, 03_random3_02.txt, 04_handmade_00.txt, 04_handmade_01.txt, 04_handmade_02.txt, 04_handmade_03.txt, 04_handmade_04.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 00_sample_00.txt | AC | 309 ms | 740956 KiB |
| 00_sample_01.txt | AC | 309 ms | 741192 KiB |
| 00_sample_02.txt | AC | 309 ms | 741192 KiB |
| 01_random_00.txt | AC | 645 ms | 792636 KiB |
| 01_random_01.txt | AC | 495 ms | 758652 KiB |
| 01_random_02.txt | AC | 330 ms | 742556 KiB |
| 01_random_03.txt | AC | 313 ms | 741368 KiB |
| 02_random2_00.txt | TLE | 5558 ms | 744112 KiB |
| 02_random2_01.txt | TLE | 5554 ms | 743044 KiB |
| 02_random2_02.txt | TLE | 5557 ms | 751068 KiB |
| 02_random2_03.txt | TLE | 5557 ms | 744640 KiB |
| 02_random2_04.txt | TLE | 5559 ms | 746516 KiB |
| 02_random2_05.txt | TLE | 5557 ms | 751444 KiB |
| 02_random2_06.txt | TLE | 5557 ms | 747216 KiB |
| 02_random2_07.txt | TLE | 5557 ms | 746832 KiB |
| 02_random2_08.txt | TLE | 5557 ms | 745640 KiB |
| 02_random2_09.txt | TLE | 5557 ms | 747144 KiB |
| 02_random2_10.txt | TLE | 5558 ms | 755468 KiB |
| 02_random2_11.txt | TLE | 5565 ms | 938916 KiB |
| 02_random2_12.txt | TLE | 5557 ms | 745600 KiB |
| 02_random2_13.txt | TLE | 5557 ms | 745896 KiB |
| 02_random2_14.txt | TLE | 5557 ms | 745176 KiB |
| 02_random2_15.txt | TLE | 5554 ms | 745732 KiB |
| 02_random2_16.txt | TLE | 5557 ms | 745592 KiB |
| 02_random2_17.txt | TLE | 5557 ms | 745788 KiB |
| 02_random2_18.txt | TLE | 5557 ms | 745456 KiB |
| 02_random2_19.txt | TLE | 5557 ms | 745932 KiB |
| 02_random2_20.txt | TLE | 5557 ms | 745700 KiB |
| 02_random2_21.txt | TLE | 5556 ms | 745624 KiB |
| 02_random2_22.txt | TLE | 5560 ms | 745728 KiB |
| 02_random2_23.txt | TLE | 5557 ms | 745516 KiB |
| 03_random3_00.txt | TLE | 5557 ms | 745548 KiB |
| 03_random3_01.txt | TLE | 5557 ms | 745608 KiB |
| 03_random3_02.txt | TLE | 5557 ms | 745524 KiB |
| 04_handmade_00.txt | AC | 311 ms | 741012 KiB |
| 04_handmade_01.txt | AC | 314 ms | 741336 KiB |
| 04_handmade_02.txt | AC | 312 ms | 741476 KiB |
| 04_handmade_03.txt | TLE | 5557 ms | 745476 KiB |
| 04_handmade_04.txt | TLE | 5555 ms | 747124 KiB |