只需要一个数据结构对字符串进行存储,以及查询是否存在即可。
#include<cstdio> #include<cstdlib> #include<iostream> #include<string> #include<set> #include<algorithm> #include<vector> #include<queue> #include<list> #include<cmath> #include<cstring> #include<map> #include<stack> using namespace std; #define INF 0x3f3f3f3f #define maxn 5005 #define ull unsigned long long #define ll long long #define mod 9 set<string> se; int main(){ freopen("a.in","r",stdin); freopen("b.out","w",stdout); string s; while(cin >> s && s[0] != '*'){ int len = s.length(); bool f = true; for(int i = 0;i <= len - 2;++i){ string str; for(int j = 0;j + i + 1 < len;++j){ str += s[j]; str += s[j + i + 1]; if(se.find(str) != se.end()){ f = false; break; } se.insert(str); str.clear(); } se.clear(); if(!f) break; } if(f) cout << s << " is surprising." << endl; else cout << s << " is NOT surprising." << endl; } return 0; }