题解 P1738 【洛谷的文件夹】
这是个集合的题目,因为大家都知道set可以自动去重,我们就借助这个特性去做。
for循环n次,每次读入这个字符串然后遍历每个字符,是'/'就insert,因为一开始也是'/'那就输出size() - 1,还要注意的是因为最后不是'/',循环完要再insert一次:
#include <iostream>//cin cout头文件
#include <cstring>//string头文件,只用的话可加可不加,如果是像要用find,erase,length这些就要加上
#include <set>//set头文件
using namespace std;
set <string> st;
string s;
int main()
{
int n;
cin >> n;
for(int i = 1; i <= n; i++)
{
cin >> s;
string x = "";
for(auto j : s)
{
if(j == '/')
{
st.insert(x);
}
x += j;
}
st.insert(x);
cout << st.size() - 1 << endl;
}
return 0;
}
那这个循环auto是啥呢?对于这个字符串,auto j : s可以起到遍历每个字符的作用,更多细节自行百度

浙公网安备 33010602011771号