0pts求调
查看原帖
0pts求调
844213
carbon_monoxide楼主2025/1/18 23:01

MnZn刚学字典树,0pts,求调!

#include<bits/stdc++.h>
using namespace std;
struct node{
    int str[26],sum;
}s[1000];
string str1;
int tot,ans,ff;
void built(){
    int t=0;
    for(int i=0;i<str1.size();i++){
        if(str1[i]=='-'){ff=1;continue;}
        if(!s[t].str[str1[i]-'0'])
        s[t].str[str1[i]-'0']=tot++;
        t=s[t].str[str1[i]-'0'];
        s[t].sum=str1[i]-'0';
    }
}
int query(){
   int t=0,s1=0;
   for(int i=0;i<str1.size();i++){
        if(str1[i]=='-') continue;
        if(!s[t].str[str1[i]-'0']) return s1;
        t=s[t].str[str1[i]-'0'];
        s1=s1*10+s[t].sum;
   }
   return s1;
}
int main(){
	int n;cin>>n;
    for(int i=1;i<=n;i++){
        ff=0;
	    int j=i;
	    while(j){
	     	char c=char(j%10+'0');
	    	str1=c+str1;
	    	j/=10;
	    }
        built();
        if(ff) ans-=query();
        else ans+=query();
        str1="";
	}
	return cout<<ans,0;
}
2025/1/18 23:01
加载中...