您的位置:首页 > 其它

USACO2.3 五道题目

2011-04-20 17:46 260 查看
【prefix】

/*
ID: wangqia6
TASK: prefix
LANG: C++
*/

#include <fstream>
#include <string>
#include <cstring>
using namespace std;

const long PNUM = 99901;
const long LMAX = 200005;

ifstream cin ("prefix.in");
ofstream cout ("prefix.out");
bool hash[PNUM],f[LMAX];
long list_tot = 0,len,ans;
string data;

void prework()
{
memset(hash,0,sizeof(hash));
memset(f,0,sizeof(f));
}

long calc_hash(string x)
{
long len = x.size(),tmp = 0;

for (long i = 0; i < len; i++)
tmp = ((tmp << 2) + long (x[i])) % PNUM;

return tmp;
}

void initdata()
{
string tmp;

cin >> tmp;
while (tmp != ".")
{
hash[calc_hash(tmp)] = true;
cin >> tmp;
}

cin >> data;
while (cin >> tmp)
data += tmp;
len = data.size();

cin.close();
return;
}

void solve()
{
long big = len > 10 ? 10 : len,k,j,i;

for (j = 1; j <= big; j++)
{
string tmp (data,0,j);
k = calc_hash(tmp);
if (hash[k])
{
f[j - 1] = true;
ans  = j;
}
}

for (i = ans; i < len; i++)
if (! f[i])
{
for (j = 1; j <= 10; j++)
if (i - j < 0)
break;
else if (! f[i - j])
continue;
else
{
string tmp (data,i + 1 - j,j);
k = calc_hash(tmp);
if (hash[k])
{
f[i] = true;
ans  = i + 1;
break;
}
}
}

return;
}

void outitdata()
{
cout << ans << endl;
cout.close();
return;
}

int main()
{
prework();
initdata();
solve();
outitdata();
return 0;
}


【cow pedigrees】

/*
ID: wangqia6
TASK: nocows
LANG: C++
*/

#include <fstream>
#include <cstring>
using namespace std;

const long PNUM = 9901;
const long NMAX = 205;
const long HMAX = 105;

long f[NMAX][HMAX];
long n,h,i,j,k;

int main()
{
ifstream cin ("nocows.in");
ofstream cout ("nocows.out");

cin >> n >> h;

memset(f,0,sizeof(f));
for (i = 1; i <= h; i++)
f[1][i] = 1;

for (i = 3; i <= n; i+= 2)
for (j = 1; j <= h; j++)
for (k = 1; k <= i - 2; k++)
f[i][j] = (f[i][j] + f[k][j - 1] * f[i - 1 - k][j - 1]) % PNUM;

cout << (PNUM + f
[h] - f
[h - 1]) % PNUM << endl;

cin.close();
cout.close();
return 0;
}


【zero sum】

/*
ID: wangqia6
TASK:zerosum
LANG: C++
*/

#include <fstream>
using namespace std;

ifstream cin ("zerosum.in");
ofstream cout ("zerosum.out");
int n,plan[10];

void outit()
{
for (int i = 1; i < n; i++)
{
cout << i;
if (plan[i] == 1)
cout << ' ';
else if (plan[i] == 2)
cout << '+';
else
cout << '-';
}

cout << n << endl;

return;
}

void dfs(int dep,long now_ans,long now_num,bool flag)
{
if (dep == n)
{
if (flag)
now_ans += now_num;
else
now_ans -=now_num;

if (now_ans == 0)
outit();

return;
}

for (int k = 1; k <= 3; k++)
{
plan[dep] = k;
if (k == 1)
dfs(dep + 1,now_ans,now_num * 10 + dep + 1,flag);
else if ((k == 2) && (flag))
dfs(dep + 1,now_ans + now_num,dep + 1,true);
else if ((k == 2) && (! flag))
dfs(dep + 1,now_ans - now_num,dep + 1,true);
else if ((k == 3) && (flag))
dfs(dep + 1,now_ans + now_num,dep + 1,false);
else if ((k == 3) && (! flag))
dfs(dep + 1,now_ans - now_num,dep + 1,false);
}

return;
}

int main()
{
cin >> n;
dfs(1,0,1,true);

cin.close();
cout.close();
return 0;
}


【money systems】

/*
ID: wangqia6
TASK: money
LANG: C++
*/

#include <fstream>
#include <cstring>
using namespace std;

long long f[10005];

int main()
{
ifstream cin ("money.in");
ofstream cout ("money.out");

memset(f,0,sizeof(f));
f[0] = 1;

long m,n,a,i,j;
cin >> m >> n;

for (i = 1; i <= m; i++)
{
cin >> a;
for (j = a; j <= n; j++)
f[j] += f[j - a];
}

cout << f
<< endl;

cin.close();
cout.close();
return 0;
}


【controlling companies】

/*
ID: wangqia6
TASK: concom
LANG: C++
*/

#include <fstream>
#include <cstring>
using namespace std;

const int NMAX = 105;

ifstream cin ("concom.in");
ofstream cout ("concom.out");
int data[NMAX][NMAX];
long head = 0,tail = -1;
bool vis[NMAX][NMAX];
struct
{
int x,y;
} que[NMAX * NMAX];

void push(int a,int b)
{
if ((a == b) || (vis[a][b]))
return;

vis[a][b] = true;
tail ++;
que[tail].x = a;
que[tail].y = b;

return;
}

int main()
{
int n;
cin >> n;

memset(vis,0,sizeof(vis));
memset(data,0,sizeof(data));

int i,j,k,a,b,w;
for (i = 0; i < n; i++)
{
cin >> a >> b >> w;
data[a][b] = w;
if (w > 50)
push(a,b);
}

while (head <= tail)
{
i = que[head].x;
j = que[head].y;

for (k = 1; k < NMAX; k++)
{
data[i][k] += data[j][k];
if (data[i][k] > 50)
push(i,k);
}

head++;
}

for (i = 1; i < NMAX; i++)
for (j = 1; j < NMAX; j++)
if (vis[i][j])
cout << i << ' ' << j << endl;

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: