Proxy - greedy algorithm

Link above: https://blog.csdn.net/slient_love/article/details/104311852

Proxy - greedy algorithm

Title Description

Use a proxy server to hide the client information to a certain extent, to protect user privacy on the Internet. We know the IP address of n proxy server, use them now to access m servers. IP address and an access order of the m servers have also been given. The system used in the same time only a proxy server, and the server is not required to access the proxy server and it is the same IP address (otherwise the client information would likely be leaked). Under such conditions, to find a solution using a proxy server, the proxy switch so that the number of times less as possible.

Enter a description:

Each test data includes n + m + 2 row.
The first row contains only one integer n, the number of the proxy server.
Row 2 to row n + 1 in each row is a string representing the IP address of the proxy server. The n pairwise identical IP addresses.
N + 2 th row contains a single integer m, represents the number of servers to be accessed.
N + 3 th row to n + m + 2 lines each is a string representing the IP address of the server to be accessed, in the order given access.
Each string is a valid IP address, in the form of "xxx.yyy.zzz.www", which are part of any integer between 0 and 255. Any line of input data do not contain a space character.
Where, 1 <= n <= 1000,1 <= m <= 5000.

Output Description:

May be multiple sets of test data for each set of input data, output data of only one line, it contains an integer s, expressed in terms of access to the server process requires the least number of switching of the proxy server. The first use of a proxy server is not included in the switching times. If there is no arrangement to meet the requirements of the embodiment, -1 is output.

Example:

Input:

3
166.111.4.100
162.105.131.113
202.112.128.69
6
72.14.235.104
166.111.4.100
207.46.19.190
202.112.128.69
162.105.131.113
118.214.226.52

Output:

1
Do title ideas:

This problem better solution with the greedy algorithm. It understood meaning of the questions, only when N = 1, and the proxy server's IP address in the IP address of the server queue before the output is "-1."

Code shows:
#include<stdio.h>
#include<string.h>
#include <iostream>
using namespace std;

//每次调用,在目标串中找最长可以访问到的结点,从该结点开始再往后找最长
int solve(char a[1000][16],int n,char b[5000][16],int m)
{
    int i,j;
    int max=-1;//max为该代理服务器可以访问的最多个数-1
    //双层for循环用来判断该代理服务器是否可以访问完所有的服务器,若不能则得出可以访问到的最长节点
    for(i=0;i<n;i++){
        for(j=0;j<m;j++){
            if(!strcmp(a[i],b[j])){//相等
                if(max<j)
                    max=j;
                break;
            }
        }
        if(j == m)
            return 0;
    }
    //该种情况表示只有一个代理服务器,并且该代理服务器的IP地址与服务器的IP地址有一样的
    if(n==1&&max!=-1)
        return -1;
    //切换一次代理服务器地址
    return 1+solve(a,n,b+max,m-max);
}

int main()
{
    int n,m;
    while(cin>>n){
        char a[n][16];
        for(int i = 0;i <n ;i++)
            cin>>a[i];
        cin>>m;
        char b[m][16];
        for(int i = 0;i <m ;i++)
            cin>>b[i];
        cout<<solve(a,n,b,m);
    }
    return 0;
}
strcmp function:

Header files: string.h

strcmp(const char *s1,const char * s2)

rule

When s1 <s2, return is negative;

When s1 == s2, the return value = 0;

When s1> s2, it returns a positive number.

Special attention: strcmp (const char * s1, const char * s2) there is only compare strings, constants can be used to compare two strings, strings or arrays and constant comparison, not compare other form of digital parameters.

Released five original articles · won praise 0 · Views 128

Guess you like

Origin blog.csdn.net/slient_love/article/details/104345178
Recommended