ACM代码总结

huicpc26 ACM_PKU 代码总结

1、DP（动态规划）
/*1080-HumanGeneFunctions.cpp*/

AGTGATG
-GTTA-G

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]]);

#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<         getline(cin, str);
for(i=0;i         {
if(str[i]<=''Z''&&str[i]>=''A'')
{
j=S1.find(str[i]);
str[i]=S2[j];
}
}
cout<         getline(cin, end);
}
}
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 tree;
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 sp;
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<             continue;
}
//求出孩子节点
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<         for(i=1;i         {
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             for( j=0 ; j                 {
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]

• 本文已收录于以下专栏：

ACM 经典源代码2

• 2010年04月30日 17:41
• 441KB
• 下载

ACM代码格式

1. 题目没有说明有多少组测试数据 #include int main() { while (scanf("/*输入格式*/") != EOF) { //代码 } } 或 #in...
• Aloha_Kk
• 2017年12月03日 01:03
• 33

ACM 常用代码 都是很经典的

• 2010年04月24日 18:10
• 651KB
• 下载

acm的jar包及acm源码

• 2013年12月03日 20:29
• 329KB
• 下载

ACM题目和源代码

• 2013年10月26日 02:55
• 33KB
• 下载

大学ACM的总结

• ZCY20121105
• 2012年09月10日 16:52
• 3409

《acm课程设计》总结

《ACM程序设计》课程总结 一、为什么要选这门课: 一开始感觉acm挺高大上，也是想学到真本事，希望以后不是做一个枯燥无味的“码农”，成为一个优秀的工程师。 通过这门课提升自己，锻炼自己的算法能...
• wangran1111
• 2016年07月06日 15:32
• 1558

ACM代码库

• kongming_acm
• 2010年09月11日 21:24
• 1680

PKU ACM 1080 源代码

1080是LCS问题，关键是CreatLCSMatrix（）的设计。 /*Human Gene Functions*/ /*Description It is well known th...
• mimo9527
• 2011年01月03日 00:25
• 428

《ACM程序设计》课程总结

• WiseDoge
• 2016年06月26日 15:05
• 2194

举报原因： 您举报文章：ACM代码总结 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)