-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.cpp
More file actions
87 lines (87 loc) · 2.16 KB
/
string.cpp
File metadata and controls
87 lines (87 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <cstdio>
#include <cstring>
#include <ostream>
namespace sjtu
{
template<int N>
class string
{
public:
char s[N+1];
int len;
string(){memset(s,0,sizeof(s)),len=0;}
string(const std::string &a)
{
memset(s,0,sizeof(s));
len=a.length();
for(int i=0;i<len;i++) s[i]=a[i];
}
string(const char *a)
{
memset(s,0,sizeof(s));
len=strlen(a);
memcpy(s,a,len);
}
int length() const {return len;}
char& operator [] (int a) {return s[a];}
const char& operator [] (int a) const {return s[a];}
template<int M>
bool operator < (const string<M> &a) const
{
int mnl=std::min(len,a.len);
for(int i=0;i<mnl;i++) if(s[i]!=a.s[i]) return s[i]<a.s[i];
return len<a.len;
}
template<int M>
bool operator == (const string<M> &a) const
{
if(len!=a.len) return false;
for(int i=0;i<len;i++) if(s[i]!=a.s[i]) return false;
return true;
}
template<int M>
bool operator != (const string<M> &a) const {return !(*this==a);}
bool operator == (const std::string &a) const
{
if(len!=a.length()) return false;
for(int i=0;i<len;i++) if(s[i]!=a[i]) return false;
return true;
}
bool operator != (const std::string &a) const {return !(*this==a);}
string<N> operator = (const string<N> &a)
{
if(this==&a) return *this;
len=a.len;
memcpy(s,a.s,len);
return *this;
}
string<N> operator = (const std::string &a)
{
len=a.length();
for(int i=0;i<len;i++) s[i]=a[i];
return *this;
}
template<int M>
friend std::ostream& operator << (std::ostream &os,const string<M> &p);
int hash(int base=263,int MOD=998244353) const
{
int ret=0;
for(int i=0;i<len;i++) ret=((1ll*base*ret+s[i])%MOD+MOD)%MOD;
return ret;
}
};
template<int M>
std::ostream& operator << (std::ostream &os,const string<M> &p)
{
for(int i=0;i<p.len;i++) os<<p[i];
return os;
}
template<int N>
int stoi(const string<N> &p)
{
int ret=0;
for(int i=0;i<p.len;i++) ret=ret*10+p[i]-'0';
return ret;
}
}