129 条题解
-
0Jessy LV 8 @ 2016-09-03 19:27:15
//算是打表吧!
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
using namespace std;void makenext(const char p[],int next[])//不理解就自己代一代
{
int q,k;
int m=strlen(p);
next[0]=0;
for(q=1,k=0;q<m;++q)
{
while(k>0&&p[q]!=p[k])
k=next[k-1];
if(p[q]==p[k])
{
k++;//查找与前面有相同的,即前后缀有相同的元素,于是就加1
}
next[q]=k;
}
}int kmp(const char t[],const char p[],int next[])
{
int n,m;
int i,q;
n=strlen(t);
m=strlen(p);
makenext(p,next);
for(i=0,q=0;i<n;++i)
{
while(q>0&&p[q]!=t[i])
q=next[q-1];
if(p[q]==t[i])
{
q++;
}
if(q==m)
printf("%d\n",(i-m+2));
}
}int main()
{
int next[20]={0};
char T[300]={
49,50,51,52,53,54,55,56,57,49,48,49,49,49,50,49,51,49,52,
49,53,49,54,49,55,49,56,49,57,50,48,50,49,50,50,50,51,50,
52,50,53,50,54,50,55,50,56,50,57,51,48,51,49,51,50,51,51,
51,52,51,53,51,54,51,55,51,56,51,57,
52,48,52,49,52,50,52,51,52,52,52,53,52,54,52,55,52,56,52,57,
53,48,53,49,53,50,53,51,53,52,53,53,53,54,53,55,53,56,53,57,
54,48,54,49,54,50,54,51,54,52,54,53,54,54,54,55,54,56,54,57,
55,48,55,49,55,50,55,51,55,52,55,53,55,54,55,55,55,56,53,57,
};
char P[30];
cin.getline(P,30);
int l=strlen(P);
//for(int i=0;i<l;i++)printf("%d ",P[i]);
kmp(T,P,next);
return 0;
} -
02016-07-13 14:26:14@
暴力做法:只能过22个点
~~~
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
string A,s,t;
int main(){
for(int i=1;i<=10000;i++){
stringstream ss;
ss<<i;ss>>t;s+=t;
}
getline(cin,A);
cout<<s.find(A)+1<<endl;
return 0;
}
发几个c++的参考代码膜拜一下。Orz。
#include <iostream> #include <cstring> #include <string> #include <cstdio> #include <cstdlib> #include <algorithm> using namespace std; class BigNum{ private: enum {blen=4,MAX=53,base=10000}; int len; int a[MAX]; public: explicit BigNum(int num=0); BigNum(const BigNum& b); BigNum(int x,int b); explicit BigNum(string num); BigNum operator+ (const BigNum& b)const; BigNum operator+ (const int b)const; BigNum operator- (const BigNum& b)const; BigNum operator- (const int b)const; BigNum operator* (const BigNum& b)const; BigNum operator* (const int b)const; const int& operator[] (int i)const; int& operator[] (int i); bool operator< (const BigNum& b)const; bool operator> (const BigNum& b)const; bool operator== (const BigNum& b)const; bool operator!= (const BigNum& b)const; string toString()const; friend ostream& operator<< (ostream& os,const BigNum& a); }; BigNum::BigNum(int num){ if (num==0){ len=0; memset(a,0,sizeof(a)); return; } len=0; memset(a,0,sizeof(a)); while (num>0){ a[len++]=num%base; num/=base; } } BigNum::BigNum(int x,int b){ // x*10^b if (x==0){ len=0; memset(a,0,sizeof(a)); return; } len=(b+1)/blen+1; if ((b+1)%blen==0) len--; memset(a,0,sizeof(a)); int i=b%blen,j=1; while (i>0){ j*=10; i--; } a[len-1]=x*j; } BigNum::BigNum(string num){ const int nn[blen]={1,10,100,1000}; if (num==string(num.size(),'0')){ len=0; memset(a,0,sizeof(a)); return; } memset(a,0,sizeof(a)); len=0; for (int i=0;i<int(num.size());i++){ if (i%blen==0) len++; a[len-1]+=(num[num.size()-i-1]-'0')*nn[i%blen]; } } BigNum::BigNum(const BigNum& b){ len=b.len; memcpy(a,b.a,sizeof(b.a)); } int& BigNum::operator[](int i){ return a[i]; } const int& BigNum::operator[](int i)const{ return a[i]; } BigNum BigNum::operator+(const BigNum& b)const{ BigNum c=BigNum(); int k=0; c.len=max(len,b.len); for (int i=0;i<c.len;i++){ c[i]=(a[i]+b[i]+k)%base; k=(a[i]+b[i]+k)/base; } while (k>0){ c[c.len++]=k%base; k/=base; } return c; } BigNum BigNum::operator+(const int b)const{ BigNum c=*this; int i=0; c[0]+=b; while (c[i]>=base){ c[i+1]+=c[i]/base; c[i]%=base; i++; } while (c[c.len]>0) c.len++; return c; } BigNum BigNum::operator-(const BigNum& b)const{ //ensure that a>b BigNum c=BigNum(); c.len=max(len,b.len); int k=0; for (int i=0;i<len;i++) if (a[i]-b[i]-k>=0) c[i]=a[i]-b[i]-k; else{ k++; c[i]=a[i]-b[i]+base; } while (c.len>0 && c[c.len-1]==0) c.len--; return c; } BigNum BigNum::operator-(const int b)const{ BigNum c=*this; int i=0; c[i]-=b; while (c[i]<0){ c[i+1]--; c[i]+=base; i++; } while (c.len>0 && c[c.len-1]==0) c.len--; return c; } BigNum BigNum::operator*(const BigNum& b)const{ BigNum c=BigNum(); c.len=0; int t; for (int j=0;j<b.len;j++) for (int i=0;i<len;i++){ t=c[i+j]+a[i]*b[j]; c[i+j]=t%base; c[i+j+1]+=t/base; if (c[i+j]>0) c.len=max(c.len,i+j); if (c[i+j+1]>0) c.len=max(c.len,i+j+1); } c.len++; return c; } BigNum BigNum::operator*(const int b)const{ BigNum c=BigNum(); c.len=len; int k=0; for (int i=0;i<len;i++){ c[i]=(a[i]*b+k)%base; k=(a[i]*b+k)/base; } while (k>0){ c[c.len++]=k%base; k/=base; } return c; } bool BigNum::operator< (const BigNum& b)const{ if (len<b.len) return true; if (len>b.len) return false; for (int i=len-1;i>=0;i--) if (a[i]<b[i]) return true; else if (a[i]>b[i]) return false; return false; } bool BigNum::operator> (const BigNum& b)const{ if (len>b.len) return true; if (len<b.len) return false; for (int i=len-1;i>=0;i--) if (a[i]>b[i]) return true; else if (a[i]<b[i]) return false; return false; } bool BigNum::operator== (const BigNum& b)const{ if (len!=b.len) return false; for (int i=0;i<len;i++) if (a[i]!=b[i]) return false; return true; } bool BigNum::operator!= (const BigNum& b)const{ return !(*this==b); } ostream& operator<< (ostream& os,const BigNum& a){ int i=a.len-1; os << a[i]; for (i-=1;i>=0;i--){ if (a[i]<a.base/10) os << '0'; if (a[i]<a.base/100) os << '0'; if (a[i]<a.base/1000) os << '0'; os << a[i]; } return os; } string BigNum::toString()const{ int i=len-1; char s[MAX*blen+1]; sprintf(s,"%d",a[i]); for (i-=1;i>=0;i--) { if (a[i]<base/10) sprintf(s+strlen(s),"%d",0); if (a[i]<base/100) sprintf(s+strlen(s),"%d",0); if (a[i]<base/1000) sprintf(s+strlen(s),"%d",0); sprintf(s+strlen(s),"%d",a[i]); } return string(s,s+strlen(s)); } BigNum cal(BigNum a){ string s=a.toString(); int x=s[0]-'0'; int b=s.size(); if (b==1) return BigNum(x); BigNum tmp; for (int i=0;i<b-1;i++) tmp=tmp+BigNum(9,i)*(i+1); tmp=tmp+(a-BigNum(1,b-1)+1)*b; return tmp; } int get(string s1,string s2){ int i,ans=0; for (i=1;i<=int(min(s1.size(),s2.size()));i++){ string ss1=s1.substr(0,i); string ss2=s2.substr(s2.size()-i,i); if (ss1==ss2) ans=i; } return ans; } int main(){ //freopen("in.txt","r",stdin); int i,j,len,n,k,slen; bool ff; BigNum ans,tmp; string s,ss,st,sp,s1,s2; cin >> s; n=s.size(); if (s==string(n,'0')){ ans=cal(BigNum(1,n))-n+1; cout << ans << endl; return 0; } if (n==1){ cout << s << endl; return 0; } for (len=1;len<n;len++) for (i=0;i+len-1<n;i++){ j=i+len-1; if (j>=n) continue; ff=true; st=s.substr(i,len); if (st[0]=='0') continue; if (i>0){ sp=(BigNum(st)-1).toString(); ss=s.substr(0,i); if (ss.size()>sp.size()) continue; if (sp.rfind(ss)!=sp.size()-ss.size()) ff=false; } if (!ff) continue; slen=len; for (k=j+1;k+slen-1<n;k+=slen){ st=(BigNum(st)+1).toString(); slen=st.size(); if (k+slen-1>=n) break; if (st!=s.substr(k,slen)){ ff=false; break; } } if (!ff) continue; if (k<n){ st=(BigNum(st)+1).toString(); ss=s.substr(k,n-k); if (st.find(ss)!=0) ff=false; } if (ff){ st=s.substr(i,len); ans=cal(BigNum(st))-i-st.size()+1; goto ppp; } } ppp: for (i=0;i<n;i++){ st=s.substr(0,i); st=(BigNum("1"+st)+1).toString(); st=st.substr(1,st.size()-1); s2=s.substr(i,n-i); k=get(st,s2); if (k>=int(s2.size())) continue; st=s.substr(i,n-i-k)+st; if (st[0]=='0') continue; tmp=cal(BigNum(st)-1)-i+1; if (ans==BigNum(0)) ans=tmp; else if (tmp<ans) ans=tmp; } if (s[0]>'0'){ tmp=cal(BigNum(s))-n+1; if (ans==BigNum(0)) ans=tmp; else if (tmp<ans) ans=tmp; } else{ s="1"+s; tmp=cal(BigNum(s))-n; if (ans==BigNum(0)) ans=tmp; else if (tmp<ans) ans=tmp; } cout<< ans << endl; }
#include<cstdio> #include<cstring> class BigNum{ public: BigNum(char *str,int _len){ memset(a,0,sizeof(a)); len=_len; for(int i=len-1;i>=0;i--) a[i]=(*str++)-'0'; } BigNum(int n){ memset(a,0,sizeof(a)); for(len=0;n;n/=10,len++) a[len]=n%10; } operator bool() { return len>0; } BigNum operator++(){ a[0]++; int i; for(i=0;i<len && a[i]>9;i++) { a[i]-=10; a[i+1]++; } if(i==len) { a[i]=1; len++; } return *this; } void inc() { a[0]++; for(int i=0;i<len && a[i]>9;i++) { a[i]-=10; a[i+1]++; } } friend BigNum operator+(BigNum a,BigNum b) { if(a.len<b.len) a.len=b.len; for(int i=0;i<a.len;i++) a.a[i]+=b.a[i]; a.ceil(); return a; } friend BigNum operator+(BigNum a,int b) { return a+(BigNum)b; } friend BigNum operator-(BigNum a,BigNum b) { for(int i=a.len-1;i>0;i--) { a.a[i]-=b.a[i]; a.a[i]--; a.a[i-1]+=10; } a.a[0]-=b.a[0]; a.ceil(); return a; } friend BigNum operator-(BigNum a,int b) { return a-(BigNum)b; } friend BigNum operator*(BigNum a,BigNum b) { BigNum c=0; c.len=a.len+b.len; for(int i=0;i<a.len;i++) for(int j=0;j<b.len;j++) c.a[i+j]+=a.a[i]*b.a[j]; c.ceil(); return c; } friend BigNum operator*(BigNum a,int b) { return a*(BigNum)b; } friend bool operator<(BigNum a,BigNum b) { if(a.len==b.len) { int i; for(i=a.len-1;i>0 && a.a[i]==b.a[i];i--); return a.a[i]<b.a[i]; } else return a.len<b.len; } friend bool find(BigNum a,char *str) { for(int i=a.len-1;i>=0 && *str;i--) if(a.a[i]!=(*str++)-'0') return false; return true; } void print() { for(int i=len-1;i>=0;i--) printf("%d",a[i]); printf("\n"); } int a[300],len; private: void ceil() { for(int i=0;i<len;i++) { a[i+1]+=a[i]/10; a[i]%=10; } len++; while(len && !a[len-1]) len--; } }; bool pd(char *a) { BigNum t=9,now=0; for(char *p=a;*p;p++,t=t*10+9) { if(*p!='0') return false; now=now*10+t; } now=now+2; now.print(); return true; } char a[210]; void work() { int len=strlen(a); BigNum t=0,now=0; if(pd(a)) return; for(int i=1;i<=len;i++,t=t*10+9) { now=now+t; BigNum ans=0; for(int j=0;j<i;j++) { if(a[j]=='0') continue; BigNum prev(a,j); prev.inc(); if(j>0 && !find(prev,a+i)) continue; char nn[210]; int k; for(k=0;k<i && a[j+k];k++) nn[k]=a[j+k]; for(;k<i;k++) nn[k]=prev.a[i-k-1]+'0'; BigNum n(nn,i),temp=n; int p; for(p=j+i;p<len && find(++n,a+p);p+=n.len); if(p>=len) { temp=(temp-1)*i-now-j+1; if(!ans || temp<ans) ans=temp; } } if(ans) { ans.print(); break; } } } int main() { while (scanf("%s",a) != EOF) { work(); } return 0; }
-
02016-05-19 16:58:13@
-
02016-04-21 17:37:10@
测试数据 #0: Accepted, time = 796 ms, mem = 2632 KiB, score = 10
测试数据 #1: Accepted, time = 796 ms, mem = 2632 KiB, score = 10
测试数据 #2: Accepted, time = 765 ms, mem = 2632 KiB, score = 10
测试数据 #3: Accepted, time = 781 ms, mem = 2628 KiB, score = 10
测试数据 #4: Accepted, time = 781 ms, mem = 2628 KiB, score = 10
测试数据 #5: Accepted, time = 781 ms, mem = 2628 KiB, score = 10
测试数据 #6: Accepted, time = 765 ms, mem = 2632 KiB, score = 10
测试数据 #7: Accepted, time = 828 ms, mem = 2628 KiB, score = 10
测试数据 #8: WrongAnswer, time = 796 ms, mem = 2628 KiB, score = 0
测试数据 #9: Accepted, time = 796 ms, mem = 2632 KiB, score = 10
测试数据 #10: Accepted, time = 765 ms, mem = 2632 KiB, score = 10
测试数据 #11: Accepted, time = 765 ms, mem = 2628 KiB, score = 10
测试数据 #12: WrongAnswer, time = 796 ms, mem = 2628 KiB, score = 0
测试数据 #13: Accepted, time = 765 ms, mem = 2628 KiB, score = 10
测试数据 #14: Accepted, time = 765 ms, mem = 2632 KiB, score = 10
测试数据 #15: Accepted, time = 796 ms, mem = 2632 KiB, score = 10
测试数据 #16: WrongAnswer, time = 796 ms, mem = 2632 KiB, score = 0
测试数据 #17: Accepted, time = 765 ms, mem = 2632 KiB, score = 10
测试数据 #18: Accepted, time = 765 ms, mem = 2628 KiB, score = 10
测试数据 #19: Accepted, time = 796 ms, mem = 2632 KiB, score = 10
测试数据 #20: Accepted, time = 765 ms, mem = 2628 KiB, score = 10
测试数据 #21: WrongAnswer, time = 765 ms, mem = 2628 KiB, score = 0
测试数据 #22: WrongAnswer, time = 781 ms, mem = 2632 KiB, score = 0
测试数据 #23: WrongAnswer, time = 781 ms, mem = 2628 KiB, score = 0
测试数据 #24: Accepted, time = 781 ms, mem = 2628 KiB, score = 10
测试数据 #25: WrongAnswer, time = 906 ms, mem = 2628 KiB, score = 0
测试数据 #26: WrongAnswer, time = 796 ms, mem = 2628 KiB, score = 0
测试数据 #27: WrongAnswer, time = 796 ms, mem = 2632 KiB, score = 0
测试数据 #28: WrongAnswer, time = 796 ms, mem = 2628 KiB, score = 0
测试数据 #29: WrongAnswer, time = 812 ms, mem = 2632 KiB, score = 0
测试数据 #30: WrongAnswer, time = 828 ms, mem = 2632 KiB, score = 0
测试数据 #31: Accepted, time = 843 ms, mem = 2632 KiB, score = 10
测试数据 #32: WrongAnswer, time = 781 ms, mem = 2632 KiB, score = 0
测试数据 #33: WrongAnswer, time = 781 ms, mem = 2632 KiB, score = 0
测试数据 #34: WrongAnswer, time = 781 ms, mem = 2632 KiB, score = 0
测试数据 #35: Accepted, time = 765 ms, mem = 2628 KiB, score = 10
测试数据 #36: WrongAnswer, time = 812 ms, mem = 2628 KiB, score = 0
测试数据 #37: WrongAnswer, time = 765 ms, mem = 2632 KiB, score = 0
测试数据 #38: WrongAnswer, time = 765 ms, mem = 2632 KiB, score = 0
测试数据 #39: Accepted, time = 781 ms, mem = 2632 KiB, score = 10
WrongAnswer, time = 31540 ms, mem = 2632 KiB, score = 220用的c++的string的.find(),,
生成一个字符串,,然后直接输出就好了,,
但是只有220,并不知道为什么
'''
c++#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<sstream>using namespace std;
string hahawodabiao;
string inttostring(int a){
string b;
stringstream ss;
ss << a;
ss >> b;
return b;
}void makestring(){
for(int i = 1;i <= 150000; i++){
hahawodabiao += inttostring(i);
}
}int main()
{
makestring();
string a;
cin>>a;
cout<<hahawodabiao.find(a)+1;
return 0;
}'''
-
02016-03-06 19:39:37@
const maxl = 2000; type bitnode = array[0..maxl+1]of longint; var len, c,n: longint; t : array[1..maxl]of shortint; best, a, b : bitnode; procedure init; var st : string[200]; i : longint; begin readln(st); len := length(st); for i := 1 to len do begin t[i] := ord(st[i])-48; end; end; procedure dec1(var a : bitnode); var i : longint; begin i := 1; while a[i] = 0 do begin a[i] := 9; inc(i); end; dec(a[i]); if (a[0] > 1) and (a[a[0]] = 0) then dec(a[0]); end; procedure inc1(var a : bitnode); var i : longint; begin i := 1; inc(a[i]); while a[i] = 10 do begin a[i] := 0; inc(i); inc(a[i]); end; if a[a[0]+1] <> 0 then inc(a[0]); end; function match(i : longint) : boolean; var j : longint; begin if a[0] < i then begin if t[1] <> 8 then exit(false); for j := 1 to a[0] do begin if t <> a[j] then exit(false); end; exit(true); end else begin for j := 1 to i do if t <> a[j] then exit(false); exit(true); end; end; function can(i : longint) : boolean; var j : longint; begin while i < len do begin inc1(b); j := i+1; while (j <= len) and (j-i <= b[0]) do begin if b[b[0]-j+i+1] <> t[j] then exit(false); inc(j); end; inc(i, b[0]); end; exit(true); end; procedure printnum(var a : bitnode); var i : longint; begin for i := a[0] downto 1 do write(a[i]); writeln; end; procedure mul(var a : bitnode); var b, i : longint; begin b := a[0]; for i := 1 to a[0] do a[i] := a[i]*b; for i := 1 to a[0] do begin inc(a, a[i] div 10); a[i] := a[i] mod 10; end; while a <> 0 do begin inc(i); a := a[i] div 10; a[i] := a[i] mod 10; end; a[0] := i; end; procedure minus(var a, b : bitnode); var i : longint; begin for i := 1 to a[0] do begin if a[i]-b[i] < 0 then begin dec(a); inc(a[i], 10); end; dec(a[i], b[i]); end; while (a[0] > 1) and (a[a[0]] = 0) do dec(a[0]); end; procedure print; var i : longint; tem : bitnode; begin dec1(best); i := best[0]-1; mul(best); fillchar(tem, sizeof(tem), 0); while tem[0] < i do begin inc(tem[0]); tem[tem[0]] := 9; minus(best, tem); end; inc1(best); for i := 1 to c do inc1(best); printnum(best); halt; end; function bigger(var a, b : bitnode): boolean; var i : longint; begin if a[0] > b[0] then exit(true); if a[0] < b[0] then exit(false); for i := a[0] downto 1 do begin if a[i] > b[i] then exit(true); if a[i] < b[i] then exit(false); end; exit(false); end; procedure main; var l, i, j, tem : longint; flag : boolean; begin flag := true; for i := 1 to len do if t[i] <> 0 then begin flag := false; break; end; if flag then begin best[0] := len+1; best[best[0]] := 1; c := 1; print; end; for i := 1 to len do best[i] := t[len-i+1]; best[0] := len; if t[1] = 0 then begin inc(best[0]); best[best[0]] := 1; end; for i := len-len shr 1-1 downto 0 do begin flag := true; for j := 1 to i do if t[j] <> t[len-i+j] then begin flag := false; break; end; if flag then begin a[0] := len-i; tem := 0; for j := 1 to a[0] do a[j] := t[a[0]-j+1]; for j := 1 to a[0]-i do begin if (a[a[0]] <> 0) and ((i = 0) or (a[1] <> 9) or (t <> 9)) then if bigger(best, a) then begin best := a; c := tem; end; a[a[0]+1] := a[1]; for l := 1 to a[0] do a[l] := a[l+1]; a[a[0]+1] := 0; inc(tem); end; end; end; for l := 1 to best[0] do begin for i := l downto 1 do if (i < len) and (t <> 0) then begin fillchar(b, sizeof(b), 0); b[0] := l; for j := l downto 1 do begin if i+l-j+1 > len then break; b[j] := t; end; if (b[0] = 1) and (b[1] = 1) then continue; a := b; dec1(a); if match(i) and can(i+l) then begin if bigger(best, a) then begin c := a[0]-i; best := a; end; end; end; end; print; end; begin init; main; end.
-
02015-07-23 19:24:16@
#include<cstdio>
#include<cstring>
int n,fp,ansp;
char str[210],minf[210],fir[210],ans[210],temp[210],next[210],ret[210];
void swap(char&a,char&b)
{
char tmp=a;
a=b;
b=tmp;
}
void get_Adjacent(char t[],int x)
{
char s[210];
int i,flag=0,len=strlen(t);
for(i=0;i<=len;i++)
s[i]=t[i];
if(x==-1)
{
for(i=len-1;i>=0&&s[i]=='0';i--);
if(i>=0)
s[i]--;
if(len>1&&s[0]=='0')
flag=-1;
for(i=i+1;i<len;i++)
s[i]='9';
}
else
{
for(i=len-1;i>=0&&s[i]=='9';i--);
if(i>=0)
s[i]++;
else
flag=1;
for(i++;i<len;i++)
s[i]='0';
}
i=0;
if(flag==1)
next[i++]='1';
for(;i<len+flag;i++)
next[i]=s[i-flag];
next[i]='\0';
}
bool judgment(int p,int len)
{
int i ;
if(str[p]!='1')
return 0;
for(i=1;i<p;i++)
if(str[i]!='9')
return 0;
for(i=1;i+p<=n&&i<len;i++)
if(str[i+p]!='0')
return 0;
return 1;
}
void getMin(char s[],char t[])
{
int i,ls=strlen(s),lt=strlen(t);
if(t[0]=='0'||lt>ls)
return;
if(lt<ls)
{
strcpy(s,t);
ansp=fp;
return;
}
for(i=0;i<ls;i++)
{
if(s[i]>t[i])
break;
else
if(s[i]<t[i])
return;
}
strcpy(s,t);
ansp=fp;
}
void find_FirstNumber()
{
int i,j,k,l,len,cnt;
for(i=0;i<208;i++)
minf[i]='A';
minf[210-1]='\0';
for(l=1;l<=n;l++)
for(len=l,i=2;i<=len+1;i++)
{
fp=i;
if(str[i]=='0')
continue;
if(n-i+1<len)
{
if(judgment(i,len+1))
{
for(j=0;j<len;j++)
fir[j]='9';
fir[j]='\0';
getMin(minf,fir);
continue;
}
for(j=1;j<i&&str[j]=='9';j++);
if(j==i)
{
for(j=0;j+i<=n;j++)
temp[j]=str[i+j];
temp[j]='\0';
get_Adjacent(temp,-1);
k=j;
cnt=1;
while(next[j-1]=='9'&&cnt<i)
{
k--;
j--;
cnt++;
}
for(j=0;j<k;j++)
fir[j]=next[j];
for(j=1;j<i;j++)
fir[k+j-1]=str[j];
fir[k+j-1]='\0';
getMin(minf,fir);
continue;
}
for(j=1;j<i;j++)
fir[len-j]=str[i-j];
fir[len]='\0';
for(j=0;j<=len-i;j++)
fir[j]=str[i+j];
get_Adjacent(fir,1);
for(j=0;j<len&&i+j<=n;j++)
if(next[j]!=str[i+j])
break;
if(j<len&&i+j<=n)
continue;
else
{
getMin(minf,fir);
continue;
}
}
if(judgment(i,len+1))
{
len++;
for(j=1;j<len;j++)
temp[j]='0';
temp[0]='1';
temp[j]='\0';
for(j=0;j<len-1;j++)
fir[j]='9';
fir[j]='\0';
}
else
{
for(j=0;j<len;j++)
temp[j]=str[j+i];
temp[j]='\0';
get_Adjacent(temp,-1);
for(j=1;j<i;j++)
if(next[len-j]!=str[i-j])
break;
if(j<i)
continue;
strcpy(fir,next);
}
k=i+len;
while(k<=n)
{
get_Adjacent(temp,1);
len=strlen(next);
for(j=0;k+j<=n&&j<len;j++)
if(next[j]!=str[k+j])
break;
if(k+j<=n&&j<len)
break;
strcpy(temp,next);
k+=len;
}
if(k>n)
getMin(minf,fir);
}
}
void bigAdd(char a[],char b[])
{
int la=strlen(a),lb=strlen(b),i,j,carrt=0,tmp,cnt=0;
for(i=la-1,j=lb-1;i>=0&&j>=0;i--,j--)
{
tmp=a[i]+b[j]-'0'-'0'+carrt;
carrt=tmp/10;
ret[cnt++]=tmp%10+'0';
}
for(;i>=0;i--)
{
tmp=a[i]-'0'+carrt;
carrt=tmp/10;
ret[cnt++]=tmp%10+'0';
}
for(;j>=0;j--)
{
tmp=b[j]-'0'+carrt;
carrt=tmp/10;
ret[cnt++]=tmp%10+'0';
}
while(carrt)
{
ret[cnt++]=carrt%10+'0';
carrt/=10;
}
ret[cnt++]='\0';
for(i=0;i<cnt/2;i++)
swap(ret[i],ret[cnt-i-2]);
strcpy(a,ret);
}
void bigMutil(char a[],int b)
{
int i,len=strlen(a);
for(i=0;i<=len;i++)
next[i]=a[i];
for(i=1;i<b;i++)
bigAdd(a,next);
}
void cal_Temp()
{
int i,j,len=strlen(minf);
for(i=1;i<len&&minf[i]=='0';i++);
if(i==len&&minf[0]=='1')
{
temp[0]='0';
temp[1]='\0';
return;
}
j=i=1;
temp[0]=minf[0]-1;
if(temp[0]=='0')
i=0;
for(;i<=len;i++,j++)
temp[i]=minf[j];
}
void solve()
{
char a[210];
int i,j,cnt,t,len=strlen(minf);
ans[0]='0';
ans[1]='\0';
for(i=1;i<len;i++)
{
t=i*9;
cnt=0;
while(t)
{
a[++cnt]=t%10+'0';
t/=10;
}
for(j=0;j<cnt;j++)
temp[j]=a[cnt-j];
cnt=j;
for(j=0;j<i-1;j++)
temp[cnt++]='0';
temp[cnt]='\0';
bigAdd(ans,temp);
}
cal_Temp();
bigMutil(temp,len);
bigAdd(ans,temp);
for(i=-1;i<=len-ansp;i++)
{
get_Adjacent(ans,1);
strcpy(ans,next);
}
}
bool special_Judge()
{
int i,j;
n--;
for(i=1;i<=n&&str[i]=='0';i++);
if(i<=n/2+1)
return 0;
ansp=i;
if(i>n)
{
minf[0]='1';
for(j=1;j<=n;j++)
minf[j]='0';
minf[j]='\0';
}
else
{
for(j=0;i+j<=n;j++)
minf[j]=str[i+j];
for(;j<n;j++)
minf[j]='0';
minf[j]='\0';
}
return 1;
}
main()
{
str[0]='0';
scanf("%s",str+1);
n=strlen(str);
if(!special_Judge())
find_FirstNumber();
solve();
printf("%s",ans);
} -
02015-02-05 14:01:55@
这道题特别难,史诗级巨难。100行开外。不知道楼下用的是什么语言,看上去很好很简洁,但是没有大括号也挺乱。
-
02014-12-11 04:01:26@
https://github.com/gaoyunzhi/online_judge/blob/master/vijos1005.py
# https://vijos.org/p/1039
# test cases http://acm.timus.ru/forum/?space=1&num=1165
import sys, randomclass Solver():
def init(self, string):
self._string = string
self.len = len(self._string)
self.initMin()
self.updateEasyCaseMin() # where start can be extraced
self.updateHardCaseMin() # where start can not be extraceddef getAns(self):
digit = len(str(self.min))
ans = 0
for i in xrange(1, digit):
ans += 9 * i * 10 ** (i - 1)
ans += digit * (self.min - 10 ** (digit - 1))
ans -= self.pos
return ans + 1def initMin(self):
if self._string[0] == '0':
self.min = int('1' + self._string) + 1
self.pos = self.len
else:
self.min = int(self._string)
self.pos = 0def updateEasyCaseMin(self):
for start_ind in xrange(self.len):
if self._string[start_ind] == '0':
continue
for end_ind in xrange(start_ind * 2, self.len):
start = self._string[start_ind: end_ind + 1]
start_int = int(start)
if start_int > self.min or (start_int == 1 and start_ind != 0):
continue
potential_list = map(
str,
range(start_int - 1, start_int + self.len / len(start) + 1)
)
potential_string = ''.join(potential_list)
if not potential_string[len(potential_list[0]) - start_ind:] \
.startswith(self._string):
continue
self._updateMin(start_int, start_ind)def updateHardCaseMin(self):
for start_ind in xrange(1, self.len):
second_part = self._string[start_ind:]
if second_part[0] == '0':
continue
first_part = self._string[:start_ind]
second_part_int = int(second_part)
if second_part_int > self.min:
continue
for num in xrange(1, start_ind + 1):
tail = first_part[start_ind - num:]
if tail == '9' * num and second_part_int > 1 \
and str(second_part_int - 1).endswith(first_part[:start_ind - num]):
self._updateMin(int(str(second_part_int - 1) + tail) + 1, start_ind)
if tail != '9' * num and second_part.endswith(first_part[:start_ind - num]):
self._updateMin(int(second_part + tail) + 1, start_ind)def _updateMin(self, new_min, pos):
if new_min == self.min:
self.pos = max(self.pos, pos)
return
if new_min < self.min:
self.min = new_min
self.pos = posdef main():
# handle=sys.stdin
handle = open("1005.txt", "r")
string = handle.readline().strip()
sol = Solver(string)
sys.stdout.write(str(sol.getAns()))def test():
string = ''.join(map(str, xrange(1, 100000)))
print Solver('999').getAns() == 2588
print Solver(string[:100]).getAns() == 1
print Solver('1').getAns() == 1
print Solver('5').getAns() == 5
print Solver('101').getAns() == 10
print Solver('01').getAns() == 11
print Solver('1011').getAns() == 10
print Solver('12345').getAns() == 1
print Solver('012345').getAns() == 629595
print Solver('9920').getAns() == 488
print Solver('0112').getAns() == 3373for i in xrange(2000):
if random.random() > 0.005: continue
for j in xrange(i, 2000):
if random.random() > 0.005: continue
substring = string[i: j + 1]
ind = string.find(substring)
if Solver(substring).getAns() != ind + 1:
print '- attention -'
print i, j
print substring, Solver(substring).getAns()if name == '__main__':
# main()
test() -
02014-09-02 19:35:12@
var s,t,q,p,ans,v,z,da,u:string;
a,b,c,d,e,g,i,j,k,m,n,ji:longint;
h:array[1..200]of boolean;
f:array[1..256]of string;
function qian(l:string):string; //(求一个数的前面一个数,即a-1)
var o,w:longint;r:string;
begin
o:=length(l);
r:=l;
while r[o]='0' do dec(o);
r[o]:=pred(r[o]);
for w:=o+1 to length(r) do r[w]:='9';
if r[1]='0' then delete(r,1,1);
qian:=r;
end;
function hou(l:string):string;//(后一个数,即a+1)
var o,w:longint;r:string;
begin
o:=length(l);
r:=l;
while (r[o]='9')and(o>1) do dec(o);
if (o=1)and(r[1]='9') then
r:='1'+r else r[o]:=succ(r[o]);
for w:=o+1 to length(r) do r[w]:='0';
hou:=r;
end;
function sum(l1,w1:string):string;//(求两数之和)
var o,r,e1:longint;l,w,x:string;
begin
l:=l1;w:=w1;
x:='';
if length(l)<length(w) then
for o:=length(l)+1 to length(w) do l:='0'+l;
if length(l)>length(w) then
for o:=length(w)+1 to length(l) do w:='0'+w;
e1:=0;
for o:=length(w) downto 1 do
begin
r:=ord(l[o])+ord(w[o])-96+e1;
e1:=r div 10;
r:=r mod 10;
x:=chr(r+48)+x;
end;
if e1>0 then x:=chr(e1+48)+x;
sum:=x;
end;
begin
readln(s);
g:=length(s);
f[1]:='9';v:='9';
for i:=2 to 256 do
begin
v:=sum(v,'9');
f[i]:=v;
for j:=1 to i-1 do f[i]:=f[i]+'0';
end;
ans:='';
for i:=1 to 250 do ans:=ans+'0';
c:=1;
for i:=1 to length(s) do if s[i]<>'0' then begin c:=0;break;end;
if c=1 then begin ji:=1;ans:='1'+s;e:=length(ans)-1;end;//末尾全是0时特殊判断
if ji=0 then
for i:=1 to g do
begin
for j:=1 to g-i+1 do
begin
if s[j]='0' then continue;
t:=copy(s,j,i);a:=j-1;b:=j+i;
q:=t;p:=t;
while a>0 do begin q:=qian(q);
if length(q)>a then begin t:=copy(q,length(q)-a+1,a)+t;a:=0;end else
begin t:=q+t;a:=a-length(q);
if q='0' then continue;end;end;
while b<=g do begin p:=hou(p);
if length(p)>g-b+1 then begin t:=t+copy(p,1,g-b+1);b:=g+1;end else
begin t:=t+p;b:=b+length(p);end;end;
if t=s then begin ans:=copy(s,j,i);e:=j+i-1;break;end;
end;
if e>0 then break;
end;
if ji=0 then
for i:=2 to g do
begin
for j:=g downto g-i+2 do
if j-i<=0 then
begin
if s[j]='0' then continue;
t:=copy(s,j,g-j+1);
p:=copy(s,g-i+1,j-g+i-1);
q:=hou(p);
if length(q)>length(p) then
begin t:=t+copy(q,2,length(q)-1);end else t:=t+q;
c:=0;
p:=qian(t);
if copy(p,length(p)-j+2,length(p))=copy(s,1,j-1) then c:=1;
if c=0 then continue;
if length(t)<length(ans) then c:=0 else
if length(t)=length(ans) then begin if t<ans then c:=0;end else
continue;
if c=0 then begin ans:=t;e:=j-1+length(t);end;
end;
end;
if ji=0 then begin
da:='0';
ans[1]:=pred(ans[1]);
ans:=sum(ans,'1');
da:=ans;
u:='0';
for i:=1 to length(ans)-1 do u:=sum(u,f[i]);
for i:=1 to length(ans)-1 do
da:=sum(da,ans);
da:=sum(da,u);
end else
begin
da:='0';
for i:=1 to e do da:=sum(da,f[i]);
end;
if ji=0 then begin
if length(da)<6 then
begin z:=da;da:='';end else
begin z:=copy(da,length(da)-5,6);delete(da,length(da)-5,6);end;
val(z,d);
d:=d-e;
str(d,z);
da:=da+z;
end;
da:=sum(da,'1');
if ji=1 then da:=sum(da,'1');
writeln(da);
end.果然超长。。。。。
-
02014-08-20 16:52:29@
评测结果
编译成功测试数据 #0: Accepted, time = 0 ms, mem = 888 KiB, score = 10
测试数据 #1: Accepted, time = 0 ms, mem = 888 KiB, score = 10
测试数据 #2: Accepted, time = 0 ms, mem = 888 KiB, score = 10
测试数据 #3: Accepted, time = 0 ms, mem = 884 KiB, score = 10
测试数据 #4: Accepted, time = 0 ms, mem = 888 KiB, score = 10
测试数据 #5: Accepted, time = 0 ms, mem = 888 KiB, score = 10
测试数据 #6: Accepted, time = 0 ms, mem = 888 KiB, score = 10
测试数据 #7: Accepted, time = 0 ms, mem = 888 KiB, score = 10
测试数据 #8: Accepted, time = 0 ms, mem = 888 KiB, score = 10
测试数据 #9: Accepted, time = 0 ms, mem = 884 KiB, score = 10
测试数据 #10: Accepted, time = 15 ms, mem = 888 KiB, score = 10
测试数据 #11: Accepted, time = 0 ms, mem = 884 KiB, score = 10
测试数据 #12: Accepted, time = 15 ms, mem = 884 KiB, score = 10
测试数据 #13: Accepted, time = 0 ms, mem = 884 KiB, score = 10
测试数据 #14: Accepted, time = 15 ms, mem = 884 KiB, score = 10
测试数据 #15: Accepted, time = 0 ms, mem = 884 KiB, score = 10
测试数据 #16: Accepted, time = 15 ms, mem = 884 KiB, score = 10
测试数据 #17: Accepted, time = 0 ms, mem = 884 KiB, score = 10
测试数据 #18: Accepted, time = 0 ms, mem = 888 KiB, score = 10
测试数据 #19: Accepted, time = 15 ms, mem = 884 KiB, score = 10
测试数据 #20: Accepted, time = 0 ms, mem = 888 KiB, score = 10
测试数据 #21: Accepted, time = 0 ms, mem = 888 KiB, score = 10
测试数据 #22: Accepted, time = 0 ms, mem = 888 KiB, score = 10
测试数据 #23: Accepted, time = 0 ms, mem = 888 KiB, score = 10
测试数据 #24: Accepted, time = 0 ms, mem = 884 KiB, score = 10
测试数据 #25: Accepted, time = 15 ms, mem = 884 KiB, score = 10
测试数据 #26: Accepted, time = 0 ms, mem = 884 KiB, score = 10
测试数据 #27: Accepted, time = 0 ms, mem = 884 KiB, score = 10
测试数据 #28: Accepted, time = 62 ms, mem = 888 KiB, score = 10
测试数据 #29: Accepted, time = 15 ms, mem = 888 KiB, score = 10
测试数据 #30: Accepted, time = 0 ms, mem = 888 KiB, score = 10
测试数据 #31: Accepted, time = 15 ms, mem = 888 KiB, score = 10
测试数据 #32: Accepted, time = 15 ms, mem = 888 KiB, score = 10
测试数据 #33: Accepted, time = 0 ms, mem = 888 KiB, score = 10
测试数据 #34: Accepted, time = 0 ms, mem = 884 KiB, score = 10
测试数据 #35: Accepted, time = 0 ms, mem = 884 KiB, score = 10
测试数据 #36: Accepted, time = 15 ms, mem = 884 KiB, score = 10
测试数据 #37: Accepted, time = 15 ms, mem = 888 KiB, score = 10
测试数据 #38: Accepted, time = 0 ms, mem = 888 KiB, score = 10
测试数据 #39: Accepted, time = 0 ms, mem = 884 KiB, score = 10
Accepted, time = 227 ms, mem = 888 KiB, score = 400卧槽 无语了
-
02014-03-27 20:30:26@
编译成功
测试数据 #0: Accepted, time = 0 ms, mem = 800 KiB, score = 10
测试数据 #1: Accepted, time = 0 ms, mem = 804 KiB, score = 10
测试数据 #2: Accepted, time = 0 ms, mem = 808 KiB, score = 10
测试数据 #3: Accepted, time = 15 ms, mem = 804 KiB, score = 10
测试数据 #4: Accepted, time = 0 ms, mem = 804 KiB, score = 10
测试数据 #5: Accepted, time = 0 ms, mem = 804 KiB, score = 10
测试数据 #6: Accepted, time = 0 ms, mem = 800 KiB, score = 10
测试数据 #7: Accepted, time = 0 ms, mem = 800 KiB, score = 10
测试数据 #8: Accepted, time = 0 ms, mem = 800 KiB, score = 10
测试数据 #9: Accepted, time = 0 ms, mem = 804 KiB, score = 10
测试数据 #10: Accepted, time = 0 ms, mem = 804 KiB, score = 10
测试数据 #11: Accepted, time = 0 ms, mem = 800 KiB, score = 10
测试数据 #12: Accepted, time = 15 ms, mem = 800 KiB, score = 10
测试数据 #13: Accepted, time = 0 ms, mem = 804 KiB, score = 10
测试数据 #14: Accepted, time = 0 ms, mem = 804 KiB, score = 10
测试数据 #15: Accepted, time = 0 ms, mem = 804 KiB, score = 10
测试数据 #16: Accepted, time = 0 ms, mem = 804 KiB, score = 10
测试数据 #17: Accepted, time = 0 ms, mem = 800 KiB, score = 10
测试数据 #18: Accepted, time = 0 ms, mem = 804 KiB, score = 10
测试数据 #19: Accepted, time = 0 ms, mem = 800 KiB, score = 10
测试数据 #20: Accepted, time = 0 ms, mem = 800 KiB, score = 10
测试数据 #21: Accepted, time = 0 ms, mem = 800 KiB, score = 10
测试数据 #22: Accepted, time = 0 ms, mem = 804 KiB, score = 10
测试数据 #23: Accepted, time = 0 ms, mem = 800 KiB, score = 10
测试数据 #24: Accepted, time = 0 ms, mem = 800 KiB, score = 10
测试数据 #25: Accepted, time = 0 ms, mem = 804 KiB, score = 10
测试数据 #26: Accepted, time = 0 ms, mem = 800 KiB, score = 10
测试数据 #27: Accepted, time = 15 ms, mem = 800 KiB, score = 10
测试数据 #28: Accepted, time = 62 ms, mem = 804 KiB, score = 10
测试数据 #29: Accepted, time = 15 ms, mem = 800 KiB, score = 10
测试数据 #30: Accepted, time = 0 ms, mem = 804 KiB, score = 10
测试数据 #31: Accepted, time = 0 ms, mem = 804 KiB, score = 10
测试数据 #32: Accepted, time = 15 ms, mem = 800 KiB, score = 10
测试数据 #33: Accepted, time = 0 ms, mem = 804 KiB, score = 10
测试数据 #34: Accepted, time = 15 ms, mem = 804 KiB, score = 10
测试数据 #35: Accepted, time = 15 ms, mem = 804 KiB, score = 10
测试数据 #36: Accepted, time = 15 ms, mem = 800 KiB, score = 10
测试数据 #37: Accepted, time = 0 ms, mem = 800 KiB, score = 10
测试数据 #38: Accepted, time = 0 ms, mem = 800 KiB, score = 10
测试数据 #39: Accepted, time = 0 ms, mem = 804 KiB, score = 10
Accepted, time = 182 ms, mem = 808 KiB, score = 400
来秀了。。。。
超猥琐,史上最猥琐,比机器人搬重物还猥琐。。。 -
02014-03-26 18:35:47@
为啥0分?
type
numtype=record
len:longint;
num:array[1..300] of longint;
end;
var
value:array[1..200] of ansistring;
str:ansistring;
len:longint;
ans:numtype;
function convert(a:ansistring):numtype;
var
i:longint;
begin
fillchar(convert,sizeof(convert),0);
convert.len:=length(a);
for i:=1 to convert.len do
convert.num[i]:=ord(a[convert.len-i+1])-48;
end;
procedure dfs(dep,now:longint);
var
i:longint;
tmp:numtype;
procedure min(var a,b:numtype);
var
i:longint;
begin
if b.len<a.len then a:=b
else if b.len=a.len then
begin
for i:=a.len downto 1 do
if a.num[i]<>b.num[i] then break;
if a.num[i]>b.num[i] then a:=b;
end;
end;
function max(a,b:longint):longint;
begin
if a>b then max:=a
else max:=b;
end;
function last(var a:ansistring):ansistring;
var
j:longint;
begin
last:=a;
j:=length(last);
last[j]:=pred(last[j]);
while last[j]<'0' do
begin
last[j]:='9';
dec(j);
last[j]:=pred(last[j]);
end;
if last[1]='0' then delete(last,1,1);
end;
function next(var a:ansistring):ansistring;
var
j:longint;
begin
next:=a;
j:=length(next);
inc(next[j]);
while (j>1) and (next[j]>'9') do
begin
next[j]:='0';
dec(j);
inc(next[j]);
end;
if next[1]>'9' then
begin
next[1]:='0';
next:='1'+next;
end;
end;
function left(a:ansistring;var b:ansistring):boolean;
var
i:longint;
begin
if length(b)>length(a) then exit(false);
left:=true;
for i:=1 to length(b) do
if a[i]<>b[i] then
begin
left:=false;
break;
end;
end;
function right(a:ansistring;var b:ansistring):boolean;
var
i:longint;
begin
if length(b)>length(a) then exit(false);
right:=true;
for i:=1 to length(b) do
if a[length(a)-length(b)+i]<>b[i] then
begin
right:=false;
break;
end;
end;
function match(var a:ansistring;b:ansistring):ansistring;
var
tmp:ansistring;
lb,i,j:longint;
flag:boolean;
begin
lb:=length(b);
tmp:=b;
b:=next(b);
if length(b)>lb then delete(b,1,1);
for i:=1 to length(a)+1 do
if length(a)-i+1<=length(b) then
begin
flag:=true;
for j:=i to length(a) do
if a[j]<>b[j-i+1] then
begin
flag:=false;
break;
end;
if flag then
begin
match:=copy(a,1,i-1)+b;
if right(last(match),tmp) then break;
end;
end;
end;
function jia(a,b:numtype):numtype;
var
i:longint;
begin
fillchar(jia,sizeof(jia),0);
jia.len:=max(a.len,b.len);
for i:=1 to jia.len do
begin
inc(jia.num[i],a.num[i]+b.num[i]);
jia.num:=jia.num[i] div 10;
jia.num[i]:=jia.num[i] mod 10;
end;
if jia.num<>0 then inc(jia.len);
end;
function jian(a:numtype;b:longint):numtype;
var
j:longint;
begin
jian:=a;
dec(jian.num[1],b);
j:=1;
while jian.num[j]<0 do
begin
inc(jian.num[j],10);
dec(jian.num[j+1]);
if jian.num[j]>=0 then inc(j);
end;
while (jian.len>1) and (jian.num[jian.len]=0) do dec(jian.len);
end;
function cheng(a:numtype;b:longint):numtype;
var
i:longint;
begin
fillchar(cheng,sizeof(cheng),0);
for i:=1 to a.len do
cheng.num[i]:=a.num[i]*b;
for i:=1 to a.len+3 do
begin
inc(cheng.num,cheng.num[i] div 10);
cheng.num[i]:=cheng.num[i] mod 10;
end;
while (i>1) and (cheng.num[i]=0) do dec(i);
cheng.len:=i;
end;
function find(a:ansistring):numtype;
var
tmp,t:numtype;
i:longint;
begin
tmp:=convert(a);
t:=convert('9');
find:=convert('0');
for i:=1 to tmp.len-1 do
begin
find:=jia(find,cheng(t,i));
t:=cheng(t,10);
end;
dec(tmp.num[tmp.len]);
find:=jia(find,cheng(tmp,tmp.len));
find:=jia(find,convert('1'));
end;
begin
if now<=len then
begin
for i:=now to len do
if (i=len) or (str<>'0') then
begin
value[dep]:=copy(str,now,i-now+1);
if dep=1 then dfs(dep+1,i+1)
else if (dep=2) and (i=len) then dfs(dep+1,i+1)
else if dep=2 then
begin
if right(last(value[2]),value[1]) then dfs(dep+1,i+1);
end
else if (dep>=3) and (i=len) then
begin
if left(next(value[dep-1]),value[dep]) then dfs(dep+1,i+1);
end
else
begin
if next(value[dep-1])=value[dep] then dfs(dep+1,i+1);
end;
end;
end
else
begin
case dep of
2:
begin
if value[1][1]='0' then tmp:=jia(find('1'+value[1]),convert('1'))
else tmp:=find(value[1]);
end;
3:
begin
tmp:=jian(find(match(value[2],value[1])),length(value[1]))
end;
else
begin
tmp:=jian(find(value[2]),length(value[1]));
end;
end;
min(ans,tmp);
end;
end;
procedure print(var a:numtype);
var
i:longint;
begin
for i:=a.len downto 1 do
write(a.num[i]);
writeln;
end;
begin
readln(str);
len:=length(str);
ans.len:=300;
dfs(1,1);
print(ans);
end. -
02014-03-26 18:35:24@
type
numtype=record
len:longint;
num:array[1..300] of longint;
end;
var
value:array[1..200] of ansistring;
str:ansistring;
len:longint;
ans:numtype;
function convert(a:ansistring):numtype;
var
i:longint;
begin
fillchar(convert,sizeof(convert),0);
convert.len:=length(a);
for i:=1 to convert.len do
convert.num[i]:=ord(a[convert.len-i+1])-48;
end;
procedure dfs(dep,now:longint);
var
i:longint;
tmp:numtype;
procedure min(var a,b:numtype);
var
i:longint;
begin
if b.len<a.len then a:=b
else if b.len=a.len then
begin
for i:=a.len downto 1 do
if a.num[i]<>b.num[i] then break;
if a.num[i]>b.num[i] then a:=b;
end;
end;
function max(a,b:longint):longint;
begin
if a>b then max:=a
else max:=b;
end;
function last(var a:ansistring):ansistring;
var
j:longint;
begin
last:=a;
j:=length(last);
last[j]:=pred(last[j]);
while last[j]<'0' do
begin
last[j]:='9';
dec(j);
last[j]:=pred(last[j]);
end;
if last[1]='0' then delete(last,1,1);
end;
function next(var a:ansistring):ansistring;
var
j:longint;
begin
next:=a;
j:=length(next);
inc(next[j]);
while (j>1) and (next[j]>'9') do
begin
next[j]:='0';
dec(j);
inc(next[j]);
end;
if next[1]>'9' then
begin
next[1]:='0';
next:='1'+next;
end;
end;
function left(a:ansistring;var b:ansistring):boolean;
var
i:longint;
begin
if length(b)>length(a) then exit(false);
left:=true;
for i:=1 to length(b) do
if a[i]<>b[i] then
begin
left:=false;
break;
end;
end;
function right(a:ansistring;var b:ansistring):boolean;
var
i:longint;
begin
if length(b)>length(a) then exit(false);
right:=true;
for i:=1 to length(b) do
if a[length(a)-length(b)+i]<>b[i] then
begin
right:=false;
break;
end;
end;
function match(var a:ansistring;b:ansistring):ansistring;
var
tmp:ansistring;
lb,i,j:longint;
flag:boolean;
begin
lb:=length(b);
tmp:=b;
b:=next(b);
if length(b)>lb then delete(b,1,1);
for i:=1 to length(a)+1 do
if length(a)-i+1<=length(b) then
begin
flag:=true;
for j:=i to length(a) do
if a[j]<>b[j-i+1] then
begin
flag:=false;
break;
end;
if flag then
begin
match:=copy(a,1,i-1)+b;
if right(last(match),tmp) then break;
end;
end;
end;
function jia(a,b:numtype):numtype;
var
i:longint;
begin
fillchar(jia,sizeof(jia),0);
jia.len:=max(a.len,b.len);
for i:=1 to jia.len do
begin
inc(jia.num[i],a.num[i]+b.num[i]);
jia.num:=jia.num[i] div 10;
jia.num[i]:=jia.num[i] mod 10;
end;
if jia.num<>0 then inc(jia.len);
end;
function jian(a:numtype;b:longint):numtype;
var
j:longint;
begin
jian:=a;
dec(jian.num[1],b);
j:=1;
while jian.num[j]<0 do
begin
inc(jian.num[j],10);
dec(jian.num[j+1]);
if jian.num[j]>=0 then inc(j);
end;
while (jian.len>1) and (jian.num[jian.len]=0) do dec(jian.len);
end;
function cheng(a:numtype;b:longint):numtype;
var
i:longint;
begin
fillchar(cheng,sizeof(cheng),0);
for i:=1 to a.len do
cheng.num[i]:=a.num[i]*b;
for i:=1 to a.len+3 do
begin
inc(cheng.num,cheng.num[i] div 10);
cheng.num[i]:=cheng.num[i] mod 10;
end;
while (i>1) and (cheng.num[i]=0) do dec(i);
cheng.len:=i;
end;
function find(a:ansistring):numtype;
var
tmp,t:numtype;
i:longint;
begin
tmp:=convert(a);
t:=convert('9');
find:=convert('0');
for i:=1 to tmp.len-1 do
begin
find:=jia(find,cheng(t,i));
t:=cheng(t,10);
end;
dec(tmp.num[tmp.len]);
find:=jia(find,cheng(tmp,tmp.len));
find:=jia(find,convert('1'));
end;
begin
if now<=len then
begin
for i:=now to len do
if (i=len) or (str<>'0') then
begin
value[dep]:=copy(str,now,i-now+1);
if dep=1 then dfs(dep+1,i+1)
else if (dep=2) and (i=len) then dfs(dep+1,i+1)
else if dep=2 then
begin
if right(last(value[2]),value[1]) then dfs(dep+1,i+1);
end
else if (dep>=3) and (i=len) then
begin
if left(next(value[dep-1]),value[dep]) then dfs(dep+1,i+1);
end
else
begin
if next(value[dep-1])=value[dep] then dfs(dep+1,i+1);
end;
end;
end
else
begin
case dep of
2:
begin
if value[1][1]='0' then tmp:=jia(find('1'+value[1]),convert('1'))
else tmp:=find(value[1]);
end;
3:
begin
tmp:=jian(find(match(value[2],value[1])),length(value[1]))
end;
else
begin
tmp:=jian(find(value[2]),length(value[1]));
end;
end;
min(ans,tmp);
end;
end;
procedure print(var a:numtype);
var
i:longint;
begin
for i:=a.len downto 1 do
write(a.num[i]);
writeln;
end;
begin
readln(str);
len:=length(str);
ans.len:=300;
dfs(1,1);
print(ans);
end. -
02014-03-01 16:33:40@
测试数据 #0: Accepted, time = 0 ms, mem = 616 KiB, score = 10
测试数据 #1: Accepted, time = 0 ms, mem = 616 KiB, score = 10
测试数据 #2: Accepted, time = 0 ms, mem = 620 KiB, score = 10
测试数据 #3: Accepted, time = 15 ms, mem = 616 KiB, score = 10
测试数据 #4: Accepted, time = 0 ms, mem = 616 KiB, score = 10
测试数据 #5: Accepted, time = 0 ms, mem = 616 KiB, score = 10
测试数据 #6: Accepted, time = 0 ms, mem = 616 KiB, score = 10
测试数据 #7: Accepted, time = 0 ms, mem = 616 KiB, score = 10
测试数据 #8: TimeLimitExceeded, time = 1123 ms, mem = 616 KiB, score = 0
测试数据 #9: Accepted, time = 0 ms, mem = 616 KiB, score = 10
测试数据 #10: Accepted, time = 7 ms, mem = 620 KiB, score = 10
测试数据 #11: Accepted, time = 0 ms, mem = 616 KiB, score = 10
测试数据 #12: TimeLimitExceeded, time = 1014 ms, mem = 616 KiB, score = 0
测试数据 #13: Accepted, time = 15 ms, mem = 616 KiB, score = 10
测试数据 #14: Accepted, time = 0 ms, mem = 620 KiB, score = 10
测试数据 #15: Accepted, time = 0 ms, mem = 620 KiB, score = 10
测试数据 #16: Accepted, time = 858 ms, mem = 620 KiB, score = 10
测试数据 #17: Accepted, time = 7 ms, mem = 620 KiB, score = 10
测试数据 #18: Accepted, time = 0 ms, mem = 616 KiB, score = 10
测试数据 #19: Accepted, time = 0 ms, mem = 616 KiB, score = 10
测试数据 #20: Accepted, time = 0 ms, mem = 616 KiB, score = 10
测试数据 #21: TimeLimitExceeded, time = 1138 ms, mem = 620 KiB, score = 0
测试数据 #22: TimeLimitExceeded, time = 1029 ms, mem = 616 KiB, score = 0
测试数据 #23: TimeLimitExceeded, time = 1029 ms, mem = 620 KiB, score = 0
测试数据 #24: Accepted, time = 0 ms, mem = 616 KiB, score = 10
测试数据 #25: TimeLimitExceeded, time = 1076 ms, mem = 620 KiB, score = 0
测试数据 #26: TimeLimitExceeded, time = 1107 ms, mem = 616 KiB, score = 0
测试数据 #27: TimeLimitExceeded, time = 1092 ms, mem = 620 KiB, score = 0
测试数据 #28: TimeLimitExceeded, time = 1076 ms, mem = 620 KiB, score = 0
测试数据 #29: TimeLimitExceeded, time = 1045 ms, mem = 616 KiB, score = 0
测试数据 #30: TimeLimitExceeded, time = 1076 ms, mem = 616 KiB, score = 0
测试数据 #31: Accepted, time = 0 ms, mem = 616 KiB, score = 10
测试数据 #32: TimeLimitExceeded, time = 1014 ms, mem = 616 KiB, score = 0
测试数据 #33: TimeLimitExceeded, time = 1107 ms, mem = 616 KiB, score = 0
测试数据 #34: TimeLimitExceeded, time = 1123 ms, mem = 620 KiB, score = 0
测试数据 #35: Accepted, time = 0 ms, mem = 620 KiB, score = 10
测试数据 #36: Accepted, time = 93 ms, mem = 616 KiB, score = 10
测试数据 #37: TimeLimitExceeded, time = 1076 ms, mem = 616 KiB, score = 0
测试数据 #38: TimeLimitExceeded, time = 1107 ms, mem = 616 KiB, score = 0
测试数据 #39: Accepted, time = 0 ms, mem = 620 KiB, score = 10 -
02014-03-01 16:33:13@
program p1004;
var a:array[0..500] of integer;
s,s1:string;
ls:integer;
sum,num:longint;
//
procedure init;
begin
assign(input,'p1004.in');assign(output,'p1004.out');
reset(input);rewrite(output);
read(s);ls:=length(s);
end;
//
procedure main;
var i,k:integer;
t:boolean;
begin
k:=0;
for i:=2 to ls do
begin
while (s[k+1]<>s[i]) and (k<>0) do k:=a[k];
if (s[k+1]=s[i]) then inc(k);
a[i]:=k;
end;
k:=0;t:=false;
while not(t) do
begin
inc(num);str(num,s1);
for i:=1 to length(s1) do
begin
while (k<>0) and (s1[i]<>s[k+1]) do k:=a[k];
if (s1[i]=s[k+1]) then inc(k);
inc(sum);
if k=ls then
begin
t:=true;break;
end;
end;
end;
end;
//
procedure print;
begin
write(sum-ls+1);
close(output);
end;
begin
init;
main;
print;
end. -
02014-03-01 16:32:55@
KMP为何就多过了一个点?
-
02014-02-22 21:29:27@
测试数据 #0: Accepted, time = 0 ms, mem = 684 KiB, score = 10
测试数据 #1: Accepted, time = 0 ms, mem = 684 KiB, score = 10
测试数据 #2: Accepted, time = 0 ms, mem = 688 KiB, score = 10
测试数据 #3: Accepted, time = 15 ms, mem = 684 KiB, score = 10
测试数据 #4: Accepted, time = 0 ms, mem = 684 KiB, score = 10
测试数据 #5: Accepted, time = 0 ms, mem = 684 KiB, score = 10
测试数据 #6: Accepted, time = 0 ms, mem = 684 KiB, score = 10
测试数据 #7: Accepted, time = 15 ms, mem = 684 KiB, score = 10
测试数据 #8: Accepted, time = 7 ms, mem = 684 KiB, score = 10
测试数据 #9: Accepted, time = 0 ms, mem = 684 KiB, score = 10
测试数据 #10: Accepted, time = 0 ms, mem = 684 KiB, score = 10
测试数据 #11: Accepted, time = 15 ms, mem = 684 KiB, score = 10
测试数据 #12: Accepted, time = 15 ms, mem = 684 KiB, score = 10
测试数据 #13: Accepted, time = 0 ms, mem = 688 KiB, score = 10
测试数据 #14: Accepted, time = 0 ms, mem = 684 KiB, score = 10
测试数据 #15: Accepted, time = 7 ms, mem = 688 KiB, score = 10
测试数据 #16: Accepted, time = 7 ms, mem = 684 KiB, score = 10
测试数据 #17: Accepted, time = 0 ms, mem = 684 KiB, score = 10
测试数据 #18: Accepted, time = 0 ms, mem = 688 KiB, score = 10
测试数据 #19: Accepted, time = 0 ms, mem = 684 KiB, score = 10
测试数据 #20: Accepted, time = 7 ms, mem = 684 KiB, score = 10
测试数据 #21: Accepted, time = 0 ms, mem = 688 KiB, score = 10
测试数据 #22: Accepted, time = 15 ms, mem = 684 KiB, score = 10
测试数据 #23: Accepted, time = 0 ms, mem = 684 KiB, score = 10
测试数据 #24: Accepted, time = 15 ms, mem = 684 KiB, score = 10
测试数据 #25: Accepted, time = 15 ms, mem = 684 KiB, score = 10
测试数据 #26: Accepted, time = 15 ms, mem = 688 KiB, score = 10
测试数据 #27: Accepted, time = 0 ms, mem = 684 KiB, score = 10
测试数据 #28: Accepted, time = 78 ms, mem = 688 KiB, score = 10
测试数据 #29: Accepted, time = 15 ms, mem = 684 KiB, score = 10
测试数据 #30: Accepted, time = 15 ms, mem = 684 KiB, score = 10
测试数据 #31: Accepted, time = 15 ms, mem = 684 KiB, score = 10
测试数据 #32: Accepted, time = 15 ms, mem = 688 KiB, score = 10
测试数据 #33: Accepted, time = 0 ms, mem = 684 KiB, score = 10
测试数据 #34: Accepted, time = 0 ms, mem = 684 KiB, score = 10
测试数据 #35: Accepted, time = 15 ms, mem = 684 KiB, score = 10
测试数据 #36: Accepted, time = 7 ms, mem = 684 KiB, score = 10
测试数据 #37: Accepted, time = 7 ms, mem = 684 KiB, score = 10
测试数据 #38: Accepted, time = 7 ms, mem = 688 KiB, score = 10
测试数据 #39: Accepted, time = 0 ms, mem = 684 KiB, score = 10
Accepted, time = 322 ms, mem = 688 KiB, score = 400
我只想说:
数据为何长了又长,。。。。。。是人性的扭曲还是道德的沦丧?请看史诗级灾难片《P1005 超长数字串之数据》! -
02013-12-21 21:42:48@
........
-
02013-11-14 21:04:41@
楼下的程序超时
-
02013-11-03 11:11:47@
program p1005;
var inf,outf:text;
s,a:ansistring;
////////////////////////////////
procedure init;
beginreadln(s);
end;
///////////////////////////////
procedure main;
var l,ll,lll,n,p,kk,k,i,kkk:longint;
ss:string;
begin
l:=length(s);
n:=0;p:=1; kk:=1;k:=1;ll:=0;
while p<=l do
begin
inc(n);
ss:='';
str(n,ss);
for i:=1 to length(ss) do
begin
inc(ll);
delete(a,ll,1);
insert(ss[i],a,ll);
if s[p]=a[kk] then
begin
inc(p);
if p>l then break;
inc(kk);
end
else begin
inc(k);
p:=1;
kk:=k;
end;
end;
end;
writeln(k);end;
//////////////////////////////
begin
init;
main;
end.