苏州大学ICPC集训队新生赛第二场

A - Score

 UVA - 1585

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin>>n;
    while(n--){
    int sum=0;
    string s;
    cin>>s;
    int len=s.size();
    int tmp=0;
    for(int i=0;i<len;i++){
    if(s[i]=='O')sum+=tmp,tmp++;
    else {
    sum+=tmp;
    tmp=0;
    }
    }
    sum+=tmp;
    cout<<sum<<endl;
    }   
    return 0;
}
View Code

B - Tetrahedron

题意:四面体,从D走n-1步回到D点,问你有多少种走法,数据量1e7;

标准错误解法:广搜:

#include<iostream>
#include<queue>
using namespace std;
#define rep(i,j,k) for(int i=(int)j;i<=(int)k;i++)
#define per(i,j,k) for(int i=(int)k;i>=(int)j;i--)
#define pb push_back
#define pf push_front
#define fi first
#define se second 11
typedef long long ll;
typedef unsigned long long ull;
typedef long double ldb;
typedef double db;
// const db PI=acos(-1.0);
const ll INF=0x3f3f3f3f3f3f3f3fLL;
const int inf=0x3f3f3f3f;//0x7fffffff;
const double eps=1e-9;
const ll MOD=1e9+7;
const int maxn=1e3+5;
ll n,ans;
struct node{int id;ll step;node(int x,ll y){id=x,step=y;}};
void bfs(){
    queue<node>Q;
    Q.push(node(4,0));
    while(!Q.empty()){
    node tmp=Q.front();
    Q.pop();
    if(tmp.step==n&&tmp.id==4){
    ans++;
    }
    if(tmp.id==1&&tmp.step<n){
    int t=(tmp.step+1)%MOD;
    Q.push(node(2,t));
    Q.push(node(3,t));
    Q.push(node(4,t));
    }
    if(tmp.id==2&&tmp.step<n){
        int t=(tmp.step+1)%MOD;
    Q.push(node(3,t));
    Q.push(node(4,t));
    Q.push(node(1,t));
    }
    if(tmp.id==3&&tmp.step<n){
    int t=(tmp.step+1)%MOD;
    Q.push(node(1,t));
    Q.push(node(2,t));
    Q.push(node(4,t));
    
    }
    
    if(tmp.id==4&&tmp.step<n){
    int t=(tmp.step+1)%MOD;
    Q.push(node(1,t));
    Q.push(node(2,t));
    Q.push(node(3,t));
    }
    }
}
int main(){
    cin>>n;
    ans=0;
    bfs();
    cout<<ans<<endl;
    // system("pause");
    return 0;
}
View Code

fyh正解:dp;

这个很像dp,定义状态:  dp(i,j)为走i步,到了 j 点 ;

每走一步都受前面状态影响:转移方程为:    dp[i][j]+=dp[i-1][k];   

#include<bits/stdc++.h>
using namespace std;
#define rep(i,j,k) for(int i=(int)j;i<=(int)k;i++)
#define per(i,j,k) for(int i=(int)k;i>=(int)j;i--)
#define pb push_back
#define pf push_front
#define fi first
#define se second 11
typedef long long ll;
typedef unsigned long long ull;
typedef long double ldb;
typedef double db;
// const db PI=acos(-1.0);
const ll INF=0x3f3f3f3f3f3f3f3fLL;
const int inf=0x3f3f3f3f;//0x7fffffff;
const double eps=1e-9;
const ll MOD=1e9+7;
const int maxn=1e7+5;
int dp[maxn][4];
int main(){
   int n;
   cin>>n;
   dp[0][0]=0;
   dp[1][1]=1;
   dp[1][2]=1;
   dp[1][3]=1;
   for(int i=2;i<=n;i++)
   for(int j=0;j<=3;j++){
   for(int k=0;k<=3;k++){
   if(j==k)continue;
   dp[i][j]+=dp[i-1][k];       
   // else dp[i][j]=
   dp[i][j]%=MOD;
   }
   }
   cout<<dp[n][0]<<endl;
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/littlerita/p/12375313.html