弄了半天也没通过,搜索下才发现是题意模糊: 在输出子目录时,子目录的位置实际上没有变化,比如:a/b,在输出时,b前面应该有两个空格,而不是一个!!
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <algorithm>
char* s[10];
charpath[50];
boolcmp(chars1[],chars2[])
{
intresult = strcmp(s1, s2);
if(result < 0)returntrue;
elsereturnfalse;
}
voidProcess(inti)
{
intj, k;
intlen = strlen(s[i]);
//int high = 0;
for(j = 0, k = 0; j<len;j++)
{
if(s[i][j] =='\\')
{
s[i][j] = '\0';
boolnewNode = true;
if(i > 0)
{
char* p = strstr(s[i-1], s[i]);
if(p && (!s[i-1][j] || s[i-1][j]=='\\'))
{
newNode = false;
}
}
if(newNode)
{
strcpy(&path[k], &s[i][k]);
printf("%s\n", path);
}
//path[high] = ' ';
//high++;
s[i][j] = '\\';
while(k < j+1){path[k]=' '; k++;}
k = j+1;
}
}
if(k < j)
{
boolnewNode = true;
if(i > 0)
{
char* p = strstr(s[i-1], s[i]);
if(p && (!s[i-1][j] || s[i-1][j]=='\\'))
{
newNode = false;
}
}
if(newNode)
{
strcpy(&path[k], &s[i][k]);
printf("%s\n", path);
}
}
}
intmain()
{
intN;
inti;
while(scanf("%d", &N) != EOF && N != 0)
{
for(i = 0;i < N;i++)
{
s[i] = (char*)malloc(sizeof(char)* 50);
scanf("%s", s[i]);
}
std::sort(s, s+N, cmp);
for(i =0; i< N; i++)
{
Process(i);
}
printf("\n");
for(i = 0;i < N;i++)
{
free(s[i]);
}
}
return0;
}
/**************************************************************
Problem: 1090
User: phoenix198425
Language: C++
Result: Accepted
Time:0 ms
Memory:1020 kb
****************************************************************/