Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 60;
- int dp[N + 1][N + 1][N + 1][N + 1], len[5];
- char *str[5];
- bool notZero(int a, int b, int c, int d){
- return a != 0 && b != 0 && c != 0 && d != 0;
- }
- bool notZero(int a, int b, int c){
- return a != 0 && b != 0 && c != 0;
- }
- bool notZero(int a, int b){
- return a != 0 && b != 0;
- }
- int main(){
- for(int i = 1; i <= 4; ++i){
- str[i] = new char[N + 1];
- scanf(" %s", str[i] + 1);
- len[i] = strlen(str[i] + 1);
- for(int j = 1; j <= len[i]; ++j){
- str[i][j] = tolower(str[i][j]);
- }
- }
- for(int a = 0; a <= len[1]; ++a){
- for(int b = 0; b <= len[2]; ++b){
- for(int c = 0; c <= len[3]; ++c){
- for(int d = 0; d <= len[4]; ++d){
- if(a == 0 && b == 0 && c == 0 && d == 0){
- dp[a][b][c][d] = 0;
- continue;
- }
- int mn = 1e9;
- // Four Parts
- if(notZero(a, b, c, d) && str[1][a] == str[2][b] && str[2][b] == str[3][c] && str[3][c] == str[4][d]){
- mn = min(mn, 1 + dp[a - 1][b - 1][c - 1][d - 1]);
- }
- // Three Parts
- if(notZero(a, b, c) && str[1][a] == str[2][b] && str[2][b] == str[3][c]){
- mn = min(mn, 1 + dp[a - 1][b - 1][c - 1][d]);
- }
- if(notZero(a, b, d) && str[1][a] == str[2][b] && str[2][b] == str[4][d]){
- mn = min(mn, 1 + dp[a - 1][b - 1][c][d - 1]);
- }
- if(notZero(a, c, d) && str[1][a] == str[3][c] && str[3][c] == str[4][d]){
- mn = min(mn, 1 + dp[a - 1][b][c - 1][d - 1]);
- }
- if(notZero(b, c, d) && str[2][b] == str[3][c] && str[3][c] == str[4][d]){
- mn = min(mn, 1 + dp[a][b - 1][c - 1][d - 1]);
- }
- // Two Parts
- if(notZero(a, b) && str[1][a] == str[2][b]){
- mn = min(mn, 1 + dp[a - 1][b - 1][c][d]);
- }
- if(notZero(a, c) && str[1][a] == str[3][c]){
- mn = min(mn, 1 + dp[a - 1][b][c - 1][d]);
- }
- if(notZero(a, d) && str[1][a] == str[4][d]){
- mn = min(mn, 1 + dp[a - 1][b][c][d - 1]);
- }
- if(notZero(b, c) && str[2][b] == str[3][c]){
- mn = min(mn, 1 + dp[a][b - 1][c - 1][d]);
- }
- if(notZero(b, d) && str[2][b] == str[4][d]){
- mn = min(mn, 1 + dp[a][b - 1][c][d - 1]);
- }
- if(notZero(c, d) && str[3][c] == str[4][d]){
- mn = min(mn, 1 + dp[a][b][c - 1][d - 1]);
- }
- // One Part
- if(a != 0){
- mn = min(mn, 1 + dp[a - 1][b][c][d]);
- }
- if(b != 0){
- mn = min(mn, 1 + dp[a][b - 1][c][d]);
- }
- if(c != 0){
- mn = min(mn, 1 + dp[a][b][c - 1][d]);
- }
- if(d != 0){
- mn = min(mn, 1 + dp[a][b][c][d - 1]);
- }
- dp[a][b][c][d] = mn;
- }
- }
- }
- }
- cout << dp[len[1]][len[2]][len[3]][len[4]];
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement