1001
hash+逆元,1A
1002
i个1的时候,考虑新加入一个1,自成1个,也可以和倒数第二个1合一起,所以是f[i]=f[i-1]+f[i-2],n<=200,大数,n有0的情况
1003
字典树,直接字典树上插入,删除,标记字符串结尾即可,查询时如果没有路走了就return false
ps.开头想复杂了,真是愚蠢
#include <map> #include <set> #include <stack> #include <queue> #include <cmath> #include <string> #include <vector> #include <cstdio> #include <cctype> #include <cstring> #include <sstream> #include <cstdlib> #include <iostream> #include <algorithm> #pragma comment(linker,"/STACK:102400000,102400000") using namespace std; #define MAX 100005 #define MAXN 1000005 #define maxnode 10 #define sigma_size 2 #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define lrt rt<<1 #define rrt rt<<1|1 #define middle int m=(r+l)>>1 #define LL long long #define ull unsigned long long #define mem(x,v) memset(x,v,sizeof(x)) #define lowbit(x) (x&-x) #define pii pair<int,int> #define bits(a) __builtin_popcount(a) #define mk make_pair #define limit 10000 //const int prime = 999983; const int INF = 0x3f3f3f3f; const LL INFF = 0x3f3f; const double pi = acos(-1.0); const double inf = 1e18; const double eps = 1e-9; const LL mod = 1e9+7; const ull mxx = 1333331; /*****************************************************/ inline void RI(int &x) { char c; while((c=getchar())<'0' || c>'9'); x=c-'0'; while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0'; } /*****************************************************/ int ch[3000005][30]; int val[3000005]; int cnt; int sz; void init(){ sz=1;mem(ch[0],0); } void inser(char *s){ int u=0; int len=strlen(s); for(int i=0;i<len;i++){ int c=s[i]-'a'; if(!ch[u][c]){ mem(ch[sz],0); val[sz]=0; ch[u][c]=sz++; } u=ch[u][c]; } val[u]=1; } bool del(char *s,int len,int u){ if(len==strlen(s)-1){ ch[u][s[len]-'a']=0; } else{ if(!del(s,len+1,ch[u][s[len]-'a'])){ ch[u][s[len]-'a']=0; } } int flag=0; for(int i=0;i<26;i++){ if(ch[u][i]) flag=1; } if(flag||val[u]) return true; else return false; } bool query(char *s){ int u=0; int len=strlen(s); for(int i=0;i<len;i++){ int c=s[i]-'a'; if(!ch[u][c]) return false; u=ch[u][c]; } return true; } char a[20],b[35]; int main(){ //freopen("in.txt","r",stdin); int n; cin>>n; init(); for(int i=0;i<n;i++){ scanf("%s%s",a,b); if(a[0]=='i') inser(b); else if(a[0]=='s'){ if(query(b)) printf("Yes\n"); else printf("No\n"); } else{ if(query(b)) del(b,0,0); } } return 0; }
04
char字符串读入排序后存入string然后放进map即可
05
模拟,对于每行的条件,结构体里记录变量名,上下界,然后暴力比较
#include <map> #include <set> #include <stack> #include <queue> #include <cmath> #include <string> #include <vector> #include <cstdio> #include <cctype> #include <cstring> #include <sstream> #include <cstdlib> #include <iostream> #include <algorithm> #pragma comment(linker,"/STACK:102400000,102400000") using namespace std; #define MAX 100005 #define MAXN 1000005 #define maxnode 10 #define sigma_size 2 #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define lrt rt<<1 #define rrt rt<<1|1 #define middle int m=(r+l)>>1 #define LL long long #define ull unsigned long long #define mem(x,v) memset(x,v,sizeof(x)) #define lowbit(x) (x&-x) #define pii pair<int,int> #define bits(a) __builtin_popcount(a) #define mk make_pair #define limit 10000 //const int prime = 999983; const int INF = 0x3f3f3f3f; const LL INFF = 0x3f3f; const double pi = acos(-1.0); const double inf = 1e18; const double eps = 1e-9; const LL mod = 1e9+7; const ull mxx = 1333331; /*****************************************************/ inline void RI(int &x) { char c; while((c=getchar())<'0' || c>'9'); x=c-'0'; while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0'; } /*****************************************************/ struct Node{ string bian; int l,r; }u; vector<Node> v[1005]; int kong[1005]; char s[100005]; bool xiao(char c){ if(c>='a'&&c<='z') return true; return false; } bool fu(char c){ if(c=='='||c=='<'||c=='>') return true; return false; } bool shu(char c){ if(c>='0'&&c<='9') return true; return false; } void add(Node x,int j){ int flag=0; for(int i=0;i<v[j].size();i++){ if(v[j][i].bian==x.bian){ flag=1; if(x.r<v[j][i].l||x.l>v[j][i].r){ kong[j]=1; } else{ v[j][i].l=max(v[j][i].l,x.l); v[j][i].r=min(v[j][i].r,x.r); } break; } } if(flag==0) v[j].push_back(x); } bool check(int x,int y){ for(int i=0;i<v[x].size();i++){ for(int j=0;j<v[y].size();j++){ if(v[x][i].bian==v[y][j].bian){ if(v[x][i].l>v[y][j].r||v[x][i].r<v[y][j].l){ return false; } } } } return true; } int main(){ //freopen("in.txt","r",stdin); int n; cin>>n; getchar(); for(int i=1;i<=n;i++){ gets(s); int len=strlen(s); string b=""; string f=""; int ret=0; int flag=1; for(int j=0;j<len;j++){ if(xiao(s[j])) b=b+s[j]; if(fu(s[j])) f=f+s[j]; if(s[j]=='-') flag=-1; if(shu(s[j])) ret=ret*10+(s[j]-'0'); if(s[j]==','){ if(f=="<") u=(Node){b,-INF,flag*ret-1}; if(f==">") u=(Node){b,flag*ret+1,INF}; if(f=="==") u=(Node){b,flag*ret,flag*ret}; if(f=="<=") u=(Node){b,-INF,flag*ret}; if(f==">=") u=(Node){b,flag*ret,INF}; //cout<<u.bian<<" "<<u.l<<" "<<u.r<<endl; add(u,i); b="",f="",ret=0,flag=1; } } if(f=="<") u=(Node){b,-INF,flag*ret-1}; if(f==">") u=(Node){b,flag*ret+1,INF}; if(f=="==") u=(Node){b,flag*ret,flag*ret}; if(f=="<=") u=(Node){b,-INF,flag*ret}; if(f==">=") u=(Node){b,flag*ret,INF}; //cout<<u.bian<<" "<<u.l<<" "<<u.r<<endl; add(u,i); b="",f="",ret=0,flag=1; vector<int> ans; if(kong[i]) printf("unique\n"); else{ for(int j=1;j<i;j++){ if(kong[j]) continue; if(check(j,i)){ ans.push_back(j); } } if(ans.size()==0) printf("unique\n"); else{ for(int j=0;j<ans.size();j++){ printf("%d",ans[j]); if(j==ans.size()-1) printf("\n"); else printf(" "); } } } } return 0; }