$Hdu1381\ Crazy\ Search$

前置芝士 :string 的 基本用法

        string s = "hello world" ;
    string tmp(s,0,5) ;
    cout << tmp << endl ;

上面这一段代码 可以复制粘贴 然后改变 数字。(试试效果

\(string\ tmp(s,i,n);\)
赋初值 也就是说从 $s[i] - To -  s[i+n-1] $

所以既然是这样 应该就有一种比较优秀的做法 \(O(S.length())\)

可海星。

\[STL大法好啊\]

然后用\(map\)统计种类(\(set\)也行的。

#include <bits/stdc++.h>
#define rep(i,j,n) for(register int i=j;i<=n;i++)
#define Rep(i,j,n) for(register int i=j;i>=n;i--)
#define low(x) x&(-x)
using namespace std ;
typedef long long LL ;
const int inf = INT_MAX >> 1 ;
inline LL In() { LL res(0) , f(1) ; register char c ;
#define gc c = getchar()
    while(isspace(gc)) ; c == '-' ? f = - 1 , gc : 0 ;
    while(res = (res << 1) + (res << 3) + (c & 15) , isdigit(gc)) ;
    return res * f ;
#undef gc
}

int n ;
int c ;
string s ;
map < string , int > ans ;
inline void Ot() {
    cin >> n >> c >> s ;
    rep(i,1,s.length()-n+1) {
        string tmp(s,i-1,n) ; // from i - 1  to  (i - 1 + n -1 )
        //if(tmp.length() < n) continue ;
        ans[tmp] ++ ;
    }
    cout << ans.size() << endl ;
}
signed main() {
//  freopen("test.in","r",stdin) ;
    return Ot() , 0 ;
}
#include <bits/stdc++.h>
#define rep(i,j,n) for(register int i=j;i<=n;i++)
#define Rep(i,j,n) for(register int i=j;i>=n;i--)
#define low(x) x&(-x)
using namespace std ;
typedef long long LL ;
const int inf = INT_MAX >> 1 ;
inline LL In() { LL res(0) , f(1) ; register char c ;
#define gc c = getchar()
    while(isspace(gc)) ; c == '-' ? f = - 1 , gc : 0 ;
    while(res = (res << 1) + (res << 3) + (c & 15) , isdigit(gc)) ;
    return res * f ;
#undef gc
}

int n , c ;
string s ;
set < string > st ;
inline void Ot() {
    cin >> n >> c >> s ;
    rep(i,1,s.length()-n+1) {
        string tmp(s,i-1,n) ;
        st.insert(tmp) ;
    }
    cout << st.size() << endl ;
}
signed main() {
//  freopen("test.in","r",stdin) ;
    return Ot() , 0 ;
}

猜你喜欢

转载自www.cnblogs.com/qf-breeze/p/10700706.html