//bruteforce to make sure that I understood the problem right.
#include <bits/stdc++.h>
using namespace std ;
const int MAX = 16 ;
string arr[MAX] ;
bool mark[MAX][MAX] ;
int n ;
int dp[MAX][(1 << MAX)] ;
int solve(int last , int mask)
{
int &ret = dp[last][mask] ;
if(ret != -1)
return ret ;
ret = 0 ;
for(int i = 0 ; i < n ; ++i)
{
if(mark[i][last] == 0 || (mask & (1 << i)))
continue ;
ret = max(ret , solve(i , mask | (1 << i)) + 1) ;
}
return ret ;
}
int main()
{
memset(dp , -1 , sizeof(dp)) ;
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
cin>>n ;
for(int i = 0 ; i < n ; ++i)
cin>>arr[i] ;
for(int i = 0 ; i < n ; ++i)
{
for(int j = i+1 ; j < n ; ++j)
{
if(max(arr[i].size() , arr[j].size()) - min(arr[i].size() , arr[j].size()) >= 2)
continue ;
bool flag = true ;
int idx1 = arr[i].size()-1 , idx2 = arr[j].size()-1 ;
for(int k = 0 ; k < max(arr[i].size() , arr[j].size())-1 ; ++k)
{
if(arr[i][idx1] != arr[j][idx2])
{
flag = false ;
break ;
}
--idx1 , --idx2 ;
}
mark[i][j] = mark[j][i] = flag ;
}
}
int ans = 0 ;
for(int i = 0 ; i < n ; ++i)
ans = max(ans , solve(i , 1 << i)+1) ;
return cout<<ans<<"\n" , 0 ;
}
Compilation message
rima.cpp: In function 'int main()':
rima.cpp:46:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int k = 0 ; k < max(arr[i].size() , arr[j].size())-1 ; ++k)
~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
4480 KB |
Output is correct |
2 |
Runtime error |
11 ms |
8832 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Runtime error |
12 ms |
8832 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
4 |
Runtime error |
11 ms |
8832 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
5 |
Runtime error |
12 ms |
8960 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
6 |
Runtime error |
11 ms |
8832 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
7 |
Runtime error |
11 ms |
8832 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
8 |
Runtime error |
12 ms |
8832 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
9 |
Runtime error |
11 ms |
8832 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
10 |
Runtime error |
11 ms |
8832 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |