5 条题解
-
5
#include<bits/stdc++.h> using namespace std; #define fre(x) freopen(x".in","r",stdin),freopen(x".out","w",stdout); using LL = long long; using PII = pair<int,int>; bool check(string &x,string &y){ int n = min(x.size(),y.size()); for(int i = 0 ;i < n;i ++){ if(x[i] != y[i])return false; } return true; } int main(){ fre("string"); cin.tie(nullptr) -> ios::sync_with_stdio(false); int n; cin >> n; vector<string> s(n + 1); for(int i = 1;i <= n ;i ++){ cin >> s[i]; } LL ans = 0; deque<PII> qe; for(int i = 1;i <= n ;i ++){ while(qe.size() && !check(s[qe.front().first],s[i]))qe.pop_front(); int last = i; while(qe.size() && s[i].size() > s[qe.back().first].size()){ last = qe.back().second; qe.pop_back(); } qe.push_back({i,last}); ans += i - qe.front().second + 1; } cout << ans <<"\n"; } -
2
单调队列写法:
发现我们需要将新加入的字符串需要和已经加入的所有字符串比对是否是包含关系。
显然所有已加入字符串都是其中最长的字符串的前缀,所以和最长的字符串比对即可。
但是匹配失败后区间右指针会移动到最大串的右侧,但此时加入新字符串并不一定合法,此时我们需要知道在最长的之后的次长串,同理进行匹配。
发现在最长串左侧的短串与匹配无关,最长串右侧的需要等最长串弹出后才有可能有用,显然单调队列。
函数用于检测下标为 和 的字符串是否互为前缀(此处使用字符串哈希处理),用数组模拟双端队列可解(不推荐使用 中的 ,常数极大)
#include<bits/stdc++.h> using namespace std; #define debug(x); cerr<<__LINE__<<" : "<<#x<<" "<<x<<"\n"; typedef long long ll; typedef pair<int, int> PII; const int N=2e5+5,mod=999999937,base=233; int n; vector<vector<ll>> hah; bool check(int x, int y){ if(hah[x].size() < hah[y].size()){swap(x, y);} int op = hah[y].size() - 1; return hah[x][op] == hah[y][op]; } int main(){ freopen("string.in", "r", stdin); freopen("string.out", "w", stdout); ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>n; hah.resize(n + 1); for(int i=1;i<=n;i++){ string s; cin>>s; ll last = 0; for(int j=0;j<(int)s.size();j++){ last = (last * base % mod + s[j] - 'a') % mod; hah[i].push_back(last); } } ll ans = 0; vector<int> q(N); int f = 0, e = 0; q[0] = 1; for(int l=1,r=2;r<=n;r++){ while(e - f >= 0 && !check(q[f], r)){l = q[f] + 1; f ++;} while(e - f >= 0 && hah[r].size() >= hah[q[e]].size()){e --;} q[++ e] = r; ans += r - l; } cout<<ans + n<<"\n"; return 0; } -
1
对于区间 内的每个字符串,若该区间符合条件,那么其中所有的字符串一定都是该区间内最长的字符串的前缀。
那么我们可以枚举区间右端点。
如果该字符串可以被整合进之前的区间,就不管,否则我们以该字符串为右端点,看之前最多到哪个字符串不能整合进现在的区间里。最后是计数算贡献,一定要减去重复的贡献。
最后一步,分析时间复杂度:因为每一次都被区间中最大的字符串给阻隔,所以我们最多把每个字符串枚举 2 次,所以最后时间复杂度为 。
#include<bits/stdc++.h> #define ll long long using namespace std; const int N = 1e6 + 15; int n; bool check(const string &s,const string &t){ int n = min(s.size(),t.size()); for(int i = 0;i < n;i ++){ if(s[i] != t[i]) return false; }return true; } string str[N]; void solve(){ cin >> n; ll ans = 0;int cnt = 0; string last = ""; for(int i = 1;i <= n;i ++){ string s;cin >> s;str[i] = s; if(check(s,last)){ if(s.size() > last.size()) last = s; cnt ++; }else{ ans += 1ll * cnt * (cnt + 1) / 2; last = s;cnt = 1; for(int j = i - 1;j;j --){ if(check(last,str[j])){ if(str[j].size() > last.size()) last = str[j]; cnt ++; }else break; }ans -= cnt * (cnt - 1) / 2; } }ans += 1ll * cnt * (cnt + 1) / 2; cout << ans << '\n'; } signed main(){ freopen("string.in","r",stdin); freopen("string.out","w",stdout); ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); int T = 1; while(T --) solve(); return 0; } /* 每个点至多被扫两次,所以复杂度为 O(sum|s|) */ -
1
众所周知,字典树中任意节点的任意祖先对应的字符串一定是该节点对应字符串的前缀,所以可以把所有字符串插入字典树,在结尾位置记录该字符串下标
接着,遍历字典树,记录从根到当前节点的路径中的节点对应字符串的下标,那么显然,恰好与当前节点对应字符串的下标相邻的段才能产生贡献,那么用 set 维护就可以了
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> PII; const int N = 2e5 + 5; const int M = 1e6 + 5; int n; ll ans; string s; set<PII> se; int tr[26][M],tot; vector<int> pos[M]; void insert(string &s,int id) { int p = 0; for(char c : s) { c -= 'a'; if(!tr[c][p]) tr[c][p] = ++ tot; p = tr[c][p]; } pos[p].push_back(id); } void dfs(int p) { for(int x : pos[p]) { auto it = se.lower_bound({x,0}); int l = x,r = x; if(it != se.end() && it->first == x + 1) { r = it->second,it = se.erase(it); } if(it != se.begin() && (-- it)->second == x - 1) { l = it->first,it = se.erase(it); } ans += 1LL * (x - l + 1) * (r - x + 1); se.insert({l,r}); } for(int c = 0;c < 26;c ++) { if(tr[c][p]) dfs(tr[c][p]); } for(int x : pos[p]) { auto it = -- se.lower_bound({x + 1,0}); int l = it->first,r = it->second; se.erase(it); if(l != x) se.insert({l,x - 1}); if(r != x) se.insert({x + 1,r}); } } void solve() { cin >> n; for(int i = 1;i <= n;i ++) { cin >> s; insert(s,i); } dfs(0); cout << ans << "\n"; } signed main() { freopen("string.in","r",stdin); freopen("string.out","w",stdout); ios::sync_with_stdio(0); cin.tie(0),cout.tie(0); int t = 1; // cin >> t; while(t --) solve(); return 0; }
- 1
信息
- ID
- 21
- 时间
- 1000ms
- 内存
- 512MiB
- 难度
- 6
- 标签
- 递交数
- 40
- 已通过
- 12
- 上传者