Path resolution

Topic: The path to resolve
the meaning of problems:
Insert picture description here

Input:
Insert picture description here
Output:
Insert picture description here
Insert picture description here
Problem-solving ideas: csp T3 simulation problem, is to find a method; first consider the current directory, it must be an absolute path (not to give the relative path can not be done, do not think here), then we will not consider relative Path, only the first 60% of points are considered, and only the absolute path is included here (this is not related to the current directory, and can be normalized in the same way as the current directory is normalized). So this begins to involve how to formalize and modify according to the requirements of the topic.
Here I talk about my thoughts: First of all, I do n’t think too much about '.' A character in it), only consider the '/';
after meeting the '/', I only consider the character in front of him (regardless of the following), if the front is '/', it is ignored, otherwise continue to consider /./ and / ... /; Finally, determine whether the tail is a '/' or the string is a '/'.
For the last 40%, it can be initialized here, I let the initial string be the normalized current directory and add '/', then let the relative path given be preceded by '/', because the relative path will definitely not be '/'beginning. Then just scan it again like the absolute path.

Code:

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
string s;
bool flag=0;
string sol(string cs)
{
    string h="";
    if(flag==0)//判断绝对路径
    {
        h+='/';
        for(int i=1;i<cs.size();i++)
        {
            if(cs[i]=='/')
            {
                if(cs[i-1]=='/')
                {
                    continue;
                }else if(cs[i-1]=='.')
                {
                    if(cs[i-2]=='/'){continue;}
                    if(cs[i-2]=='.')
                    {
                        if(cs[i-3]=='/'){
                            if(h=="/"){continue;}
                            else{
                                if(h[h.size()-1]=='/')
                                {
                                    h=h.erase(h.size()-1,1);
                                }
                                for(int k=h.size()-1;h[k]!='/';k--)
                                {
                                    h=h.erase(k,1);
                                }
                                continue;
                            }
                        }
                        else{
                            h+='/';
                        }
                    }else
                    {
                        h+='/';
                    }
                }else{
                    h+='/';
                }
            }else if(cs[i]=='.')
            {
                if(cs[i-1]=='/'&&cs[i+1]=='/')
                {
                    continue;
                }else if(i+2<cs.size()&&(cs[i-1]=='/'&&cs[i+1]=='.'&&cs[i+2]=='/'))
                {
                    continue;
                }else if(i+1<cs.size()&&(cs[i+1]=='/'&&cs[i-1]=='.'&&cs[i-2]=='/'))
                {
                    continue;
                }else
                {
                    h+=".";
                }
            }else
            {
                h+=cs[i];
            }
        }
        if(h[h.size()-1]=='/'&&h!="/")
        {
            h=h.erase(h.size()-1,1);
        }
        flag=1;
        return h;
    }
    h=s+'/';
    cs='/'+cs;//相对路径初始化
    for(int i=1;i<cs.size();i++)
    {
        if(cs[i]=='/')
        {
            if(cs[i-1]=='/')
            {
                continue;
            }else if(cs[i-1]=='.')
            {
                if(cs[i-2]=='/'){continue;}
                if(cs[i-2]=='.')
                {
                    if(cs[i-3]=='/'){
                        if(h=="/"){continue;}
                        else{
                            if(h[h.size()-1]=='/')
                            {
                                h=h.erase(h.size()-1,1);
                            }
                            for(int k=h.size()-1;h[k]!='/';k--)
                            {
                                h=h.erase(k,1);
                            }
                            continue;
                        }
                    }
                    else{
                        h+='/';
                    }
                }else
                {
                    h+='/';
                }
            }else{
                h+='/';
            }
        }else if(cs[i]=='.')
        {
            if(cs[i-1]=='/'&&cs[i+1]=='/')
            {
                continue;
            }else if(i+2<cs.size()&&(cs[i-1]=='/'&&cs[i+1]=='.'&&cs[i+2]=='/'))
            {
                continue;
            }else if(i+1<cs.size()&&(cs[i+1]=='/'&&cs[i-1]=='.'&&cs[i-2]=='/'))
            {
                continue;
            }else
            {
                h+=".";
            }
        }else
        {
            h+=cs[i];
        }
    }
    if(h[h.size()-1]=='/'&&h!="/")
    {
        h=h.erase(h.size()-1,1);
    }
    return h;
}
int main()
{
    int p;
    cin>>p;
    cin>>s;
    string t=s;
    s=sol(t);
    string cnt;
    char c=getchar();
    while(p--)
    {
        getline(cin,cnt);
        if(cnt.empty())//判断是否是空的
        {
            cout<<s<<endl;
            continue;
        }
        if(cnt[0]=='/')//判断一下
        {
            flag=0;
        }
        cnt=sol(cnt);
        cout<<cnt<<endl;
    }
}
PGZ
Published 34 original articles · praised 0 · visits 869

Guess you like

Origin blog.csdn.net/qq_43653717/article/details/105254626