5 条题解
-
0
对于一个字符串的子串最多有n*(n+1)/2,这题的数据只有500,最多只有250000个子串,可以考虑用map存下所以子串的个数(由于map会对字符串自动排序,可能会很慢,所以字符串的题还是用 unordered_map吧),暴力判断即可,时间复杂度O(1)~O(n),代码很短
#include<bits/stdc++.h> using namespace std; int const N=1e5+10; unordered_map<string,int> mp; int main() { int n; cin>>n; string s1; cin>>s1; for(int i=0;i<n;i++) { string s=""; for(int j=i;j<n;j++) { s+=s1[j]; mp[s]++; } } int m,ans=0; cin>>m; for(int i=1;i<=m;i++) { string s; cin>>s; ans+=mp[s]; } cout<<ans<<endl; return 0; }
-
0
#include <bits/stdc++.h> using namespace std; const int N = 5e2+5; int n; char ch[N]; int q; int main() { int ans=0; cin>>n; cin>>ch+1; cin>>q; while(q--) { string s; cin>>s; int len=s.length(); for(int i=1;i<=n-len+1;i++) { bool flag=0; for(int j=0;j<=len-1;j++) { if(ch[i+j]!=s[j]) { flag=1; break; } } if(!flag) ans++; } } cout<<ans; return 0; }
-
0
#include<bits/stdc++.h> template<class T>inline void read(T &x){x=0;char c=getchar();bool f=0;for(;!isdigit(c);c=getchar())if(c=='-')f=1;for(;isdigit(c);c=getchar())x=(x<<3)+(x<<1)+c-'0';x=f?(~(x-1)):x;} template<class T>inline void write(T x){if(x<0)return putchar('-'),write(-x),void();if(x>=10)write(x/10);putchar(x%10+'0');return;} template<class T>inline void print(T x){write(x);putchar('\n');} template<typename type,typename ...T>inline void read(type &x,T&...y){read(x),read(y...);} using namespace std; const int N=505; int n,q; string s,t[N]; inline int check(int pos){ int ans=0; for(int i=1;i<=q;++i){ int len=t[i].length()-1; bool same=1; if(pos+len-1>n)same=0; else{ for(int j=1;pos+j-1<=n&&j<=len;++j){ if(t[i][j]!=s[pos+j-1]){ same=0; break; } } } if(same==1){ ans++; } } return ans; } int result; signed main(){ read(n); cin>>s; s=" "+s; read(q); for(int i=1;i<=q;++i){ cin>>t[i]; t[i]=" "+t[i]; } for(int i=1;i<=n;++i){ result+=check(i); } print(result); return 0; }
- 1
信息
- ID
- 958
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 6
- 标签
- (无)
- 递交数
- 161
- 已通过
- 50
- 上传者