给你一个长度为 n 的字符串数组 names 。你将会在文件系统中创建 n 个文件夹:在第 i 分钟,新建名为 names[i] 的文件夹。
由于两个文件 不能 共享相同的文件名,因此如果新建文件夹使用的文件名已经被占用,系统会以 (k) 的形式为新文件夹的文件名添加后缀,其中 k 是能保证文件名唯一的 最小正整数 。
返回长度为 n 的字符串数组,其中 ans[i] 是创建第 i 个文件夹时系统分配给该文件夹的实际名称。
示例 1:
输入:names = ["pes","fifa","gta","pes(2019)"]
输出:["pes","fifa","gta","pes(2019)"]
解释:文件系统将会这样创建文件名:
"pes" --> 之前未分配,仍为 "pes"
"fifa" --> 之前未分配,仍为 "fifa"
"gta" --> 之前未分配,仍为 "gta"
"pes(2019)" --> 之前未分配,仍为 "pes(2019)"
示例 2:
输入:names = ["gta","gta(1)","gta","avalon"]
输出:["gta","gta(1)","gta(2)","avalon"]
解释:文件系统将会这样创建文件名:
"gta" --> 之前未分配,仍为 "gta"
"gta(1)" --> 之前未分配,仍为 "gta(1)"
"gta" --> 文件名被占用,系统为该名称添加后缀 (k),由于 "gta(1)" 也被占用,所以 k = 2 。实际创建的文件名为 "gta(2)" 。
"avalon" --> 之前未分配,仍为 "avalon"
示例 3:
输入:names = ["onepiece","onepiece(1)","onepiece(2)","onepiece(3)","onepiece"]
输出:["onepiece","onepiece(1)","onepiece(2)","onepiece(3)","onepiece(4)"]
解释:当创建最后一个文件夹时,最小的正有效 k 为 4 ,文件名变为 "onepiece(4)"。
示例 4:
输入:names = ["wano","wano","wano","wano"]
输出:["wano","wano(1)","wano(2)","wano(3)"]
解释:每次创建文件夹 "wano" 时,只需增加后缀中 k 的值即可。
示例 5:
输入:names = ["kaido","kaido(1)","kaido","kaido(1)"]
输出:["kaido","kaido(1)","kaido(2)","kaido(1)(1)"]
解释:注意,如果含后缀文件名被占用,那么系统也会按规则在名称后添加新的后缀 (k) 。
提示:
1 <= names.length <= 5 * 10^4
1 <= names[i].length <= 20
names[i] 由小写英文字母、数字和/或圆括号组成。
\
首先推荐Y总的视频讲题 : https://www.bilibili.com/video/BV1ja4y1Y7nk 15分10秒左右*
- 先考虑暴力,每次找
name+后缀
是否出现过,后缀
从1
开始枚举 - 考虑优化,每次都从
1
开始枚举后缀太慢了,直接用hash
表记录name
的上一个后缀
#define debug
#ifdef debug
#include <time.h>
#include "/home/majiao/mb.h"
#endif
#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <unordered_map>
#include <map>
#include <set>
#include <stack>
#include <queue>
#define MAXN ((int)1e5+7)
#define ll long long int
#define QAQ (0)
using namespace std;
#define show(x...) \
do { \
cout << "\033[31;1m " << #x << " -> "; \
err(x); \
} while (0)
void err() { cout << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }
class Solution {
public:
#if 0
//暴力做法
vector<string> getFolderNames(vector<string>& arr) {
vector<string> ans;
unordered_set<string> table;
for(auto str : arr) {
string tmp = str;
int k = 0;
while(table.count(tmp)) {
tmp = str + "(" + to_string(++k) + ")";
}
table.insert(tmp);
ans.push_back(tmp);
}
return ans;
}
#else
vector<string> getFolderNames(vector<string>& arr) {
vector<string> ans;
unordered_map<string, int> cnt; //用哈希表记录上一个的后缀
unordered_set<string> table; //记录当前name是否出现过
for(auto str : arr) {
string tmp = str;
int k = cnt[tmp];
while(table.count(tmp)) {
tmp = str + "(" + to_string(++k) + ")";
// show(tmp, table.count(tmp), cnt[tmp]);
}
cnt[str] = k;
table.insert(tmp);
ans.push_back(tmp);
}
return ans;
}
#endif
};
#ifdef debug
signed main() {
freopen("test", "r", stdin);
clock_t stime = clock();
Solution s;
vector<string> a = {"pes","fifa","gta","pes(2019)" };
vector<string> b = {"onepiece","onepiece(1)","onepiece(2)","onepiece(3)","onepiece"};
vector<string> c = {"wano","wano","wano","wano" };
vector<string> d = {"kaido","kaido(1)","kaido","kaido(1)" };
vector<string> ans = s.getFolderNames(d);
for(auto x : ans)
cout << x << " " ;
clock_t etime = clock();
printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
return 0;
}
#endif