//09/05/23 19:56 #include <bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define str string typedef long long ll; typedef unsigned long long ull; typedef __int128 it; const int Maxn=200000+10; //快读模板 inline int read() { int ans=0,sign=1; char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch==' '){ sign=-1; } ch=getchar(); } while(ch>='0'&&ch<='9'){ ans=ans*10+ch-'0'; ch=getchar(); } return ans*sign; } inline bool Prime(int n){ if(n<2) return false; for(int i=2;i<=sqrt(n);i++){ if(n%i==0) return false; } return true; } str s[110]; int n; set<str> se; int res=0; int ans=-1; inline void dfs(str k) { res++; se.insert(k); if((int)se.size()!=res){ // for(auto k:se) cout<<k<<"->1"; // cout<<"\n"; ans=max(ans,res-1); // cout<<ans<<"\n"; return ; } bool check=false; for(int i=0;i<n-1;i++){ if(k[1]==s[i][0]){ dfs(s[i]); check=true; } } if(!check){ ans=max(ans,res); // for(auto k:se) cout<<k<<"->2"; // cout<<"\n"; // cout<<ans<<"\n"; return ; } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>n; str st; cin>>st; for(int i=0;i<n-1;i++){ str t; cin>>t; s[i]=t; } dfs(st); cout<<ans; return 0; }c++代码
时间: 2023-06-15 21:04:34 浏览: 566
这段代码实现的是一个字符串游戏,给定一个初始字符串和一些字符串,每次可以选取一个以当前字符串结尾的字符串作为下一个字符串,要求最后得到的字符串长度最长。这里使用深度优先搜索实现,每次搜索时保存已经使用过的字符串,如果新加入的字符串已经在集合中出现过,说明已经出现了环路,此时更新答案并返回。如果没有出现过,则依次搜索以该字符串结尾的字符串。最终输出答案。
此外,代码中使用了快读模板和一些常用的宏定义,具体可以参考代码注释。
相关问题
#include <bits/stdc++.h> using namespace std
这段代码是一个C++的头文件引用和命名空间的使用示例。具体来说,`#include <bits/stdc++.h>`是一个常用的头文件引用方式,它包含了C++标准库中的所有头文件。而`using namespace std`则是为了使用`std`命名空间中的标准库函数和对象,这样就可以直接使用`cout`、`cin`等标准输入输出流对象,而不需要写`std::cout`、`std::cin`。
这种写法虽然方便,但也存在一些问题。首先,包含了所有的标准库头文件可能会导致编译时间变长。其次,使用了`using namespace std`会将整个`std`命名空间中的所有标识符引入当前作用域,可能会导致命名冲突。因此,在实际开发中,建议根据需要只包含需要的头文件,并使用具体的命名空间来避免潜在的问题。
#include <bits/stdc++.h> using namespace std;
这个头文件是C++11标准引入的,它包含了所有标准库中的头文件。使用这个头文件可以方便地在一个地方包含所有需要的头文件,而不需要一个一个地包含。这个头文件通常只在竞赛中使用,因为它不是标准C++头文件,不保证在所有编译器中都能正常工作。
以下是一个使用这个头文件的示例,实现输入4个整数a、b、c、d,将它们倒序输出:
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c, d;
cin >> a >> b >> c >> d;
cout << d << ' ' << c << ' ' << b << ' ' << a << endl;
return 0;
}
```
阅读全文
相关推荐
















