经过了一段时间的努力,我再Pku上也算是有了一个阶段性的总结拉,下面是我就这段时间搞ACM来的一些代码的总结,具体的一些题目类型的总结看本Blog的相关文章。
huicpc26 ACM_PKU 代码总结
for(i=1;i }
1、DP(动态规划)
/*1080-HumanGeneFunctions.cpp*/
观察题目给出的一个最优解:
AGTGATG
-GTTA-G
将其从某一处切开,如果左边部分的分值不是最大,那么将其进行调整,使其分值变大,
则整个解分值变大,与已知的最优矛盾。所以左边部分的分值必是最大。
同理,右边也是。可见满足最优子结构的性质。考虑使用DP:
设两个DNA序列分别为s1,s2,长度分别为len1,len2,score为分值表。
f[i,j]表示子串s1[1..i]和s2[1..j]的分值。考虑一个f[i,j],我们有:
1.s1取第i个字母,s2取“-”:f[i-1,j] + score[s1[i],''-'']
2.s1取“-”,s2取第j个字母:f[i,j-1] + score[''-'',s2[j]]
3.s1取第i个字母,s2取第j个字母:f[i-1,j-1] + score[s1[i],s2[j]]
即f[i,j] = max(f[i-1,j] + score[s1[i],''-''], f[i,j-1] + score[''-'',s2[j]], f[i-1,j-1] + score[s1[i],s2[j]]);
然后考虑边界条件,这道题为i或j为0的情况。
当i=j=0时,即为f[0,0],这是在计算f[1,1]时用到的,根据f[1,1] = f[0,0] + score[s1[i], s2[j]],明显有f[0,0] = 0。
当i=0时,即为f[0,1..len2],有了f[0,0],可以用f[0,j] = f[0,j-1] + table[''-'',s2[j]]来计算。
当j=0时,即为f[1..len1,0],有了f[0,0],可以用f[i,0] = f[i-1,0] + table[s1[i],''-'']来计算。
至于计算顺序,只要保证计算f[i,j]的时候,使用到的f[i-1,j],f[i,j-1],f[i-1,j-1]都计算出来了就行了。
所谓划分阶段也就是为了达到这个目的。这样我们使用一个二重循环就可以了。*/
#include
#include
#include
using namespace std;
#define MAX(a,b,c) (a>b?a:b)>c?(a>b?a:b):c
int ctoi(char a){
int b;
if(a==''A'') b = 0;
if(a==''C'') b = 1;
if(a==''G'') b = 2;
if(a==''T'') b = 3;
if(a==''-'') b = 4;
return b;
}
int main()
{
int t,j,k,m,n;
int f1,f2,f3;
int f[101][101];
int arr[5][5]={{5,-1,-2,-1,-3},{-1,5,-3,-2,-4},{-2,-3,5,-2,-2},{-1,-2,-2,5,-1},{-3,-4,-2,-1,0}};
string a,b;
cin>>t;
while(t--){
j = k = 0;
memset(f,0,sizeof(f));
cin>>m>>a;
cin>>n>>b;
for(j=0;j<=m;j++)
{
for(k=0;k<=n;k++)
{
if(j == 0 && k == 0)
{
f[j][k] = 0;
}
else if(j==0)
{
f[j][k] = f[j][k-1] + arr[ctoi(''-'')][ctoi(b[k-1])];
}
else if(k==0)
{
f[j][k] = f[j-1][k] + arr[ctoi(a[j-1])][ctoi(''-'')];
}
else
{
f1 = f[j-1][k] + arr[ctoi(a[j-1])][ctoi( ''-'')];
f2 = f[j][k-1] + arr[ctoi( ''-'')][ctoi(b[k-1])];
f3 = f[j-1][k-1] + arr[ctoi(a[j-1])][ctoi(b[k-1])];
f[j][k] = MAX(f1,f2,f3);
}
}
}
cout<
return 0;
}
2、/*1088-滑雪.cpp*/
#include
using namespace std;
const int highest = 100000;
int high[100][100];
bool travel[100][100];
int value[100][100];
int r, c;
const int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1};
int find_rout(int x, int y){
int i, temp;
if(x < 0 || y < 0 || x >= r || y >= c)
return 0;
if(travel[x][y])
return value[x][y];
int max_rout = 1;
for(i = 0; i < 4; ++i){
if(high[x][y] > high[x + dir[i][0]][y + dir[i][1]])
{
temp = find_rout(x + dir[i][0], y + dir[i][1]);
if(max_rout < temp + 1)
max_rout = temp + 1;
}
}
travel[x][y] = true;
value[x][y] = max_rout;
return max_rout;
}
int main(){
cin >> r >> c;
int i, j;
int max_rout = 1;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j){
travel[i][j] = false;
cin >> high[i][j];
}
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j) {
int temp = find_rout(i, j);
if(temp > max_rout)
max_rout = temp;
}
cout << max_rout << endl;
return 0;
}
3、精度运算
/*2262-Goldbach''sConjecture.cpp*/
#include
#include
void doRun(){
int i,j,sum,a;
long t;
while(cin>>a){
t = 0;
if(a== 0||a>10000)
break;
sum = 0;
i = 1;
while(1){
sum += i;
if(sum>=a){
t = a-(sum-i);
break;
}
i++;
}
t = t*i;
for(j=1;j t += j*j;
}
cout< printf("%ld %ld",a,b);
if( a>b ){
n = a ;
a = b ;
b = n ;
}
s = 0;
for(i=a; i<=b; i++){
count = 1;
temp = i;
while(temp !=1){
if(temp%2 == 1)temp = 3*temp + 1;
else temp = temp/2;
count++;
}
if(count>s) s = count;
}
printf(" %ld\n",s);
}
}
int main(){
doRun();
return 0;
}
5、/*2080-Calendar.cpp*/
#include
#include
using namespace std;
int mday[2][13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31,0,31,29,31,30,31,30,31,31,30,31,30,31 };
char week[7][20] = { "Saturday","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday" };
int main ()
{
int n;
int s, y, m, d, t;
while ( 1 ) {
cin >> n ;
if ( n < 0 )
break;
d = n % 7;
n ++ ;
for ( y = s = 0 ; s < n; s += t, y ++ )
if ( y % 400 == 0 || y % 4 == 0 && y % 100 )
t = 366;
else
t = 365;
y -- ; s -= t; n -= s;
cout << y+2000 << ''-'';
t -= 365;
for ( m = 0, s = 0; s < n; m ++,s += mday[t][m] );
cout << setw(2) << setfill(''0'') << m << ''-'';
s -= mday[t][m]; n -= s;
cout << setw(2) << setfill(''0'') << n << '' '';
cout << week[d] << endl;
}
return 0;
}
6、/*1298-TheHardestProblemEver.cpp*/
#include
#include
using namespace std;
void main()
{
string S1("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
string S2("VWXYZABCDEFGHIJKLMNOPQRSTU");
string start,end,over("ENDOFINPUT");
string str;
char ch;
int i,j;
while(1)
{
getline(cin, start);
if(start.compare(over)==0)break;/*
while((ch = getchar())!=''\n'')
{
if(ch<=''Z''&&ch>=''A'')
{
j=S1.find(ch);
ch = S2[j];
}
cout<
cout<
for(i=0;i
if(str[i]<=''Z''&&str[i]>=''A'')
{
j=S1.find(str[i]);
str[i]=S2[j];
}
}
cout<
}
}
7、/*1745-Divisibility.cpp*/
#include
#include
int d[105], e[105], a, n, k;
int main ()
{
int i, p, q;
scanf ( "%d%d", &n, &k );
memset ( d, 0, k*sizeof(int) );
scanf ( "%d", &a );
a = a%k;
if ( a < 0 ) a += k;
d[a] = 1;
while ( --n )
{
scanf ( "%d", &a );
p = a%k;
q = (-a)%k;
if ( p < 0 ) p += k;
if ( q < 0 ) q += k;
memset ( e, 0, k*sizeof(int) );
for ( i = 0; i < k; i ++, p ++, q ++ )
{
if ( d[i] == 1 )
{
e[p%k] = e[q%k] = 1;
}
}
memcpy ( d, e, k*sizeof(int) );
}
printf ( d[0] ? "Divisible\n" : "Not divisible\n" );
return 0;
}
8、/*2249-BinomialShowdown.cpp*/
#include
#include
using namespace std;
int main()
{
int n,k;
double temp;
while(cin>>n>>k)
{
if(n==0&&k==0) break;
else
{
temp=1;
if(n-k>k)
{
for(int i=n-k+1;i<=n;++i)
temp*=i;
for(int j=1;j<=k;++j)
temp/=j;
cout<
else
{
for(int i=k+1;i<=n;++i)
temp*=i;
for(int j=1;j<=n-k;++j)
temp/=j;
cout<
}
}
}
9、/*2309-BST.c*/
#include
void doRun()
{
int n;
long long i,j,k,s;
long long a;
scanf("%d",&n);
for(i=0;i
scanf("%I64d",&a);
s = a;
if( (s&1)==1)
printf("%I64d %I64d\n",s,s);
else
{
j=0;
while(1)
{
if((s&1) == 1)
break;
s >>= 1;
j++;
}
k = 1;
k <<= j;
printf("%I64d %I64d\n",a-k+1,a+k-1);
}
}
}
int main()
{
doRun();
return 0;
}
10、/*1767-WhichisNext.cpp*/
#include
#include
#include
using namespace std;
long NextTreeIdentify(string t)
{
string s,t2,t001("001");
s = t;
long res;
int node,i,posof0,posof001;
bool end = true;
for(i=0;i<=s.size()-3;i++)
{
t2 = s.substr(i,3);
if(t2.compare(t001)==0)
{
posof001 = i;
break;
}
}
end = true;
for(i=posof001+3;i
if(s[i] != ''1'')
{
end = false;
break;
}
}
if(!end)
{
/*s.erase(posof001+1,2);
posof0 = s.find_first_of("0",posof001+1);
s.insert(posof0+1,"1");
s.insert(posof0+1,"0");*/
s.replace(posof001,3,"0");
posof0 = s.find_first_of("0",posof001+1);
s.replace(posof0,1,"001");
}
else
{
s[0] = ''0'';
for(i=1;i
if(i%2 == 0)
s[i] = ''1'';
else
s[i] = ''0'';
}
}
i = s.size()-1;
res = 0;
while(i>=2)
{
node = s[i]-''0'';
if(node == 1)
res += node< i--;
}
return res;
}
void doRun()
{
long res,n,t;
int node;
stack
string s;
scanf("%ld",&n);
if(n == 0||n == 4)
{
res = n;
}
else
{
t = n;
while(1)
{
node = t % 2;
tree.push(node);
if (t<=1)break;
t>>=1;
}
if(tree.size()>30)
return;
while(!tree.empty())
{
s.insert(0,(char*)(tree.top()+''0''));
tree.pop();
}
res = NextTreeIdentify(s);
}
printf("%ld\n",res);
}
int main()
{
doRun();
return 0;
}
11、/*2229-Sumsets.cpp*/
#include
#define M 1000001
int b[M];
void doRun()
{
int i;
b[1] = 1;
b[2] = 2;
n = 0;
for(i=2;i
b[i] = b[i++-1];
b[i] = (b[i-2]+b[i/2])%1000000000;
}
while(scanf("%d",&i)!=EOF)
printf("%d\n",b[i]);
}
int main()
{
doRun();
return 0;
}
12、/*2533-LongestOrderedSubsequence.c*/
#include
void process(int *a, int sum)
{
int i,j;
int f[1000],res,max;
for(i=1; i<=sum; i++)
f[i]=0;
res = 0;
for(i=sum;i>0;i--)
{
max = 0;
for(j=i+1; j<=sum; j++)
{
if(a[j]>a[i])
{
if(max
max = f[j];
}
}
}
f[i] = max + 1;
if(res
res = f[i];
}
}
printf("%d",res);
}
main()
{
int sum;
int a[1000];
int i;
scanf("%d", &sum);
for(i=1; i<=sum; i++)
scanf("%d",a+i);
process(a,sum);
return 0;
}
13、/*2545-HammingProblem.c*/
#include
#include
#include
int isPrime(long long a)
{
int flag=1 ;
long long i;
for(i=2;i
if(a%i==0&&a!=2)
{
flag=0 ;
break ;
}
}
return flag ;
}
long long getMin3(long long a,long long b,long long c)
{
long long t=a>b?b:a;
return t>c?c:t ;
}
long long getNum(long long a,long long b,long long c,long long pos)
{
long long p[3],i,j,count = 1;
long long temp[3]= {0},min;
long long * num = (long long *)malloc((pos+2)*sizeof(long long));
p[0] = a;p[1] = b;p[2] = c;
num[0] = 1;
count = 1;
while(count!=pos+1)
{
for(i=0;i<3;i++)
{
for(j=0;j
temp[i] = num[j]*p[i];
if (temp[i]>num[count-1])
break;
}
}
min = getMin3(temp[0],temp[1],temp[2]);
num[count]=min ;
count++;
}
min = num[pos];
return min;
}
int main()
{
long long p1,p2,p3,i ;
scanf("%I64d %I64d %I64d %I64d",&p1,&p2,&p3,&i);\
if(isPrime(p1)==0||isPrime(p2)==0||isPrime(p3)==0||p1==p2||p2==p3||p1==p3)
return 1;
printf("%I64d",getNum(p1,p2,p3,i));
getchar();
return 0 ;
}
14、/*2567-CodetheTree.cpp*/
#include
#include
using namespace std;
struct node
{
int parent;
int child[51];
int du;
}nd[51];
void doRun()
{
int i,j,k,len,temp,root;
char s[256],b[2];
stack
int p[51];
while(gets(s)!=NULL)
{
for(i=0;i<51;i++)
{
nd[i].du = 1;
nd[i].parent = 0;
for(j=0;j<51;j++)
nd[i].child[j] = 0;
}
len = 0;
//计算父亲节点和度
sp.push((int)''('');
for(i=1;i<(int)strlen(s);i++)
{
if(isdigit(s[i]))
{
len++;
j=0;
while(s[i]!='' ''&& s[i]!='')'')
{
b[j++] = s[i];
i++;
}
i--;
b[j] = ''\0'';
temp = atoi(b);
sp.push(temp);
if(i==1)
{
root = temp;
nd[root].du--;
}
}
else if(s[i] == ''('')
{
nd[sp.top()].du++;
sp.push((int)''('');
}
else if(s[i] == '')'')
{
while(sp.top()!=(int)''('')
{
temp = sp.top();
sp.pop();
}
sp.pop();
if(!sp.empty())
{
nd[temp].parent = sp.top();
}
else
{
nd[temp].parent = 0;
}
}
else
{
continue;
}
}
if(len <= 1)
{
if(len==1)
cout<
}
//求出孩子节点
for(j=1;j<=len;j++)
{
for(i=1; i<=len; i++)
{
if(nd[i].parent!=0 && nd[i].parent == j)
{
nd[j].child[i] = i;
}
}
}
//求出删除点的父亲节点
k=0;
for(i=1; i<=len; i++)
{
if(nd[i].du == 1)
{
if(nd[i].parent != 0)
{
p[k++] = nd[i].parent;
nd[nd[i].parent].child[i] = 0;
nd[nd[i].parent].du--;
}
else
{
temp = 0;
while(1)
{
if(nd[i].child[temp] != 0)
{
p[k++] = nd[i].child[temp];
nd[nd[i].child[temp]].parent = 0;
nd[nd[i].child[temp]].du--;
break;
}
temp++;
}
}
nd[i].du--;
i = 0;
}
}
//输出
cout<
cout<<" "<
cout<
}
int main()
{
doRun();
return 0;
}
15、/*2568-DecodetheTree.c*/
#include
#include
char * toString(int a)
{
char * b;
b = (char*)malloc(2*sizeof(char));
if(a>9)
{
b[0] = (int)(a/10)+''0'';
b[1] = (a%10) + ''0'';
b[2] = ''\0'';
}
else
{
b[0] = a + ''0'';
b[1] = ''\0'';
}
return b;
}
int toInt(char *s)
{
int a;
if(strlen(s)>1)
{
a=(s[0]-''0'')*10+(s[1]-''0'');
}
else
a = s[0]-''0'';
return a;
}
int main()
{
int num[51],f[51],i,j,k,count,a,flag;
char s[256],temp[51][256],b[2];
while(gets(s)!=NULL)
{
i=count=0;
while(i<(int)strlen(s))
{
if(s[i] == '' '')
{
i++;
continue;
}
else
{
j=0;
while(s[i]!='' '')
{
b[j++] = s[i];
i++;
}
b[j] = ''\0'';
num[count++] = toInt(b);
}
}
if(count == 0)
{
printf("(1)\n");
continue;
}
for(i=0;i<=count+1;i++)
{
strcpy(temp[i],toString(i));
}
for(i=0;i<=count+1;i++)
{
f[i] = 1;
}
for(i=0; i
f[num[i]]++;
}
for(i=0;i
a = num[i];
j=1;
while(j<=count)
{
if(f[j]==1)
{
f[j] = 0;
f[a]--;
break;
}
j++;
}
strcat(temp[a]," (");
strcat(temp[a],temp[j]);
strcat(temp[a],")");
}
printf("(%s)\n",temp[num[count-1]]);
}
return 0;
}
16、随机化问题
/*2576-TugofWar.c*/
#include
#include
#define MAX 100
int n , num[2] , sum[2] , in[2][MAX/2] ;
void input()
{
int i ;
for( i=num[0]=sum[0]=0 ; i
scanf( "%d" , &in[0][ num[0] ] ) ;
sum[0] += in[0][ num[0] ] ;
}
for( i=n/2,num[1]=sum[1]=0 ; i
scanf( "%d" , &in[1][ num[1] ] ) ;
sum[1] += in[1][ num[1] ] ;
}
}
void process()
{
int changed , i , j ;
input() ;
for( changed=0 ; ; changed=0 )
{
for( i=0 ; i
sum[0] = sum[0]-in[0][i]+in[1][j] ;
sum[1] = sum[1]-in[1][j]+in[0][i] ;
in[0][i] ^= in[1][j] ^= in[0][i] ^= in[1][j] ;/* swap */
changed = 1 ;
}
if( !changed ) break ;
}
printf( "%d %d\n" , sum[0]
}
int main()
{
while( scanf( "%d" , &n )==1 )
process() ;
return 0 ;
}
17、/*2590-steps.cpp*/
#include
int main()
{
long a,b,d;
int n,i,c,step;
cin>>n;
for(i=0;i
cin>>a>>b;
d = b-a;
c = 1;
step = 0;
while(1)
{
if (d < 2*c)
{
break;
}
else
{
d = d-2*c;
step += 2;
c++;
}
}
if(d > c)
step += 2;
else if(d <= 0)
step += 0;
else
step += 1;
cout<
return 0;
}
18、/*2605-Simplegameonagrid.cpp*/
#include
void doRun()
{
int m,n;
while(cin>>m>>n)
{
if(m
m = m^n;
n = n^m;
m = m^n;
}
if(n==1)
{
cout<<(m+1)/2;
}
else if(n%3==0 || m%3==0)
{
cout<<2 ;
}
else
{
cout<< 1;
}
cout<
}
int main()
{
doRun();
return 0 ;
}
19、/*2615-Suffidromes.c*/
#include
char a[2000],b[2000];
int al,bl ;
int Works(char*s,int sl,int n)
{
int i ;
for(i=n;i
if(s[i]!=s[sl+n-i-1])
{
return 0 ;
}
}
return 1 ;
}
main()
{
int i,j,k,x,y,z,n ;
char*s ;
int sl ;
for(;;)
{
if(!gets(a)||!gets(b))break ;
if(!strcmp(a,b))
{
printf("No Solution.\n");
continue ;
}
al=strlen(a);
bl=strlen(b);
for(x=0;a[x]==b[x];x++);
for(i=0;i<=al&&i<=bl;i++)
{
z=Works(a,al,i)+2*Works(b,bl,i);
if(z==1||z==2||(z==3&&i>x))
{
switch(z)
{
case 1 : s=a ; break ;
case 2 : s=b ; break ;
case 3 :
for(j=i-1;j>=0;j--)if(a[j]!=b[j])break ;
s=(a[j] break ;
}
for(j=i-1;j>=0;j--)putchar(s[j]);
putchar(''\n'');
break ;
}
}
if(i>al||i>bl)
{
s=(i>al)?b:a ;
sl=(i>al)?bl:al ;
if(s[i-1]==''a''&&Works(s,sl,i))
putchar(''b'');
else
putchar(''a'');
s=(i>al)?a:b ;
sl=(i>al)?al:bl ;
for(j=sl-1;j>=0;j--)putchar(s[j]);
putchar(''\n'');
}
}
}
评论