F. Find / -type f -or -type d【GDUT 2022 grade Qualifying # 2】

F. Find / -type f -or -type d

原题链接
image
image

题意

找到".eoj"结尾的"文件"(注意是".eoj"不是"eoj")

思路

One more thing, on your file system, directory is only a logical concept. This means, a directory is created only when there is a file which relies on this directory is created and a directory cannot exist without files.
The root folder alone will not be included in this list.
目录不会脱离文件单独存在,意思就是如果一个字符串不是某个字符串的子串的话,那么这个字符串一定是文件,只要查看最后是否以".eoj"结尾即可,

但是要注意的是,文件并不一定不是某个字符串的子串,比如/a.eoj和/a.eojstr中/a.eoj也可能是文件

那么我们就要再次进行讨论,如果后续所有以该字符串为子串的字符串都是文件而不是目录的话,那么就可以判定该字符串为文件,然后查看最后是否以".eoj"结尾即可,

至于判断一个字符串是否是某个字符串的子串,我们可以先进行排序,得到的序列便是目录靠前的序列,然后用find函数逐个对比即可

代码

点击查看代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<cctype>
using namespace std;

#define X first
#define Y second

typedef pair<int,int> pii;
typedef long long LL;
const char nl = '\n';
const int N = 1e6+10;
const int M = 2e5+10;
int n,m;
vector<string> v;

void solve(){
	cin >> m;
	int ans = 0;
	while(m -- ){
		string s;
		cin >> s;
		v.push_back(s);
	}
	sort(v.begin(),v.end());
	//for(auto s:v)cout << s << nl;
	for(int i = 0; i < v.size() - 1; i ++ ){
		if(v[i].size() <= 5)continue;	//不满足长度
		string t = v[i].substr(v[i].size() - 4,4);
		if(t != ".eoj")continue;
		if(v[i + 1].find(v[i]) == -1)ans ++;//i不是i+1的子串
		else{//i是i+1的子串
			int j = i + 1;
			bool f = 0;
			while(j <= v.size() - 1 && v[j].find(v[i]) != -1){
				string tt = v[j];
				for(int k = v[i].size() - 1; k <= tt.size() - 1; k ++ ){
					if(tt[k] == '/'){
						f = 1;
						break;
					}
				}
				j ++;
			}
			if(!f)ans ++;
		}
	}
	string ss = v[v.size() - 1];
	if(v[v.size() - 1].size() > 5){
		string t = v[v.size() - 1].substr(v[v.size() - 1].size() - 4,4);
		if(t == ".eoj"){
			ans ++;
		}
	}
	
	cout << ans;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);

	solve();
}



posted @ 2023-03-08 20:49  Keith-  阅读(28)  评论(0编辑  收藏  举报