AcWing 3229. 路径解析(没过,想记录一下)
原题链接
中等
作者:
把这题Ac了
,
2024-11-27 17:05:41
,
所有人可见
,
阅读 1
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
const int N = 1010,M = 11;
vector<string> str,stk,backup;
int del[N],hh = -1;
int p;
vector<string > cpy(vector<string> &a){
vector<string> backup;
for(int i = 0;i < a.size();i++)
backup.push_back(a[i]);
return backup;
}
void del_vec(vector<string> &a,int idx){
int n = a.size() - 1;
vector<string > t;
for(int i = n;i > idx;i--){
string x = a[i];
a.pop_back();
t.push_back(x);
}
a.pop_back();
while(t.size()){
string x = t[t.size() - 1];
t.pop_back();
a.push_back(x);
}
}
void cag_str(string s,int f){
vector<string> backup;
if(f)
backup = cpy(stk);
int idx = s.find('/');
if(idx){
string t;
int j = 0;
while(j < idx) t += s[j++];
backup.push_back(t);
}
for(int i = idx;i < s.size();i++){
string a;
a += s[i];
int j = i + 1;
while(j < s.size() && s[j] != '/') a += s[j++];
backup.push_back(a);
i = j - 1;
if(j >= s.size()) break;
}
// cout << "backup1" << endl;
// for(int i = 0;i < backup.size();i++){
// cout << backup[i] << ' ';
// }
// cout << endl;
int jmp = 0;
int n = backup.size();
vector<string> backup1;
for(int i = backup.size() - 1;i >= 0;i--){
string s = backup[i];
if(jmp && (s != "/.." && s != "..")){
del_vec(backup,i);
jmp--;
}else if(s.size() == 1 || (s.size() == 2 && s[0] == '/' && s[1] == '.')){
del_vec(backup,i);
}else if((s.size() == 2 && s[0] == '.' && s[1] == '.') || (s.size() == 3 && s[0] == '/' && s[1] == '.' && s[2] == '.')){
del_vec(backup,i);
jmp++;
}else{
backup1.push_back(s);
}
}
int l = 0;
for(int i = 0;i < backup1.size();i++){
for(int j = i + 1;j < backup1.size();j++){
if(backup1[i] == backup1[j]){
l = 1;
break;
}
}
if(l == 1) break;
}
if(l == 1){
string x = backup1[backup1.size() - 1];
while(backup1.size()) backup1.pop_back();
backup1.push_back(x);
}
for(int i = backup1.size() - 1;i >= 0;i--){
cout << backup1[i];
}
if(!backup1.size()) cout << '/' << endl;
else cout << endl;
}
int main(){
string s;
cin >> p >> s;
for(int i = 0;i < s.size();i++){
string a;
a += s[i];
int j = i + 1;
while(j < s.size() && s[j] != '/') a += s[j++];
stk.push_back(a);
i = j - 1;
if(j >= s.size()) break;
}
getchar();
while(p--){
int flag = 0;
string a;
getline(cin,a);
if(a == "/.."){
cout << '/' << endl;
}else if(a == ""){
for(int i = 0;i < stk.size();i++)
cout << stk[i];
cout << endl;
}else{
int tt = a.find("..") + 1;
if(tt || a.size() == 1) flag = 1;
str.push_back(a);
cag_str(a,flag);
}
}
// for(int i = 0;i < stk.size();i++)
// cout << stk[i] << endl;
return 0;
}