【2019南昌邀请赛现场赛 - J】Prefix(STLmap,思维)
題干:
yah has?n?strings?<s1?,?,sn?>, and he generates a sequence?P?by two steps:
P=<s1?,?,sn?>
Replace each?si??with all pre?xes of itself.
An example is:
the?n?strings are?< aab,ab >
?rst,?P =< aab,ab >
then,?P =< a,aa,aab,a,ab >
There are?26?positive integers d1?,?,d26??that denote the di?culty to identify lowercase English letter a,b,?,z?for yah.
The di?culty to identify a string?strstr?is (i=1∏∣str∣?dstri??)?mod??m
Now, yah wants to calculate: for each string si?(i=1,?,n), the number of strings in?P?that is pre?x of si ?and more di?cult than si?.
Input
The ?rst line contains two integers?n,mn,m(1≤n≤105,1≤m≤2×105).
The second line contains?26 positive d1?,?,d26?, 0^50≤di?≤105.
The following?n?lines, each line contains a string, the ith??lines contains si?, the sum of length of all strings is?not great than?2×105.
Output
The only line contains?n?integers, the ith??integer denoting the number of strings in?P?that is pre?x of si??but more di?cult to identify for yah.
題意:
給定n代表n個字符串,m代表模數,再給定26個字母的每個字母的權值d,再定義字符串s的值:前i個字符對應的子串的值dd[i]= dd[i-1]*(d[s[i]])? %? m 。
輸入n個字符串,求對于每個字符串,大于該字符串?的 值 的 前綴的 個數(值dd[i]= dd[i-1]*(d[s[i]])? %? m ),主要求前綴的個數
解題報告:
map暴力搞一搞就能過、、但是要真是比賽場還是老老實實寫hash吧、、萬一T了呢。
其實也可以字典樹,存前綴出現的次數。也不難想。
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<stack> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define FF first #define SS second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e5 + 5; map<string,PII> mp; int n; int d[MAX]; ll m; string s[MAX],tmp; int main() {ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);cin>>n>>m;for(int i = 0; i<26; i++) cin>>d[i];for(int i = 1; i<=n; i++) {cin>>s[i];tmp = "";int len = s[i].length();ll tmpp = 1;for(int j = 0; j<len; j++) {tmp += s[i][j];tmpp *= d[s[i][j]-'a'];tmpp %= m;auto it = mp.find(tmp);if(it == mp.end()) mp[tmp] = pm(tmpp,1);else mp[tmp].SS ++;} }for(int i = 1; i<=n; i++) {tmp = "";int ans = 0;int tar = mp[s[i]].FF;int len = s[i].length();for(int j = 0; j<len; j++) {tmp += s[i][j];if(mp[tmp].FF > tar) ans += mp[tmp].SS;}cout << ans ;if(i == n) cout << "\n";else cout << " ";//printf("%d%c",ans,i == n ?'\n' :' ');}return 0 ; }?
總結
以上是生活随笔為你收集整理的【2019南昌邀请赛现场赛 - J】Prefix(STLmap,思维)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《Valheim英灵神殿》控制台作弊代码
- 下一篇: 《循环英雄》修改游戏速度方法 怎么调整游