Merge pull request #13 from KiritoTRw/master

A few problems solved.
This commit is contained in:
Kirigaya Kazuto 2016-05-07 20:19:36 +08:00
commit cf27523718
7 changed files with 390 additions and 0 deletions

30
HDOJ/1028_cnblogs.cpp Normal file
View File

@ -0,0 +1,30 @@
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
const int M = 120 + 10;
int dp[M];
int main()
{
int n;
while (~scanf("%d", &n))
{
memset(dp, 0, sizeof(dp));
dp[0] = 1;
for (int i = 1; i <= n; i++)
{
for (int j = i; j <= n; j++)
{
dp[j] += dp[j - i];
}
}
printf("%d\n", dp[n]);
}
return 0;
}

142
HDOJ/1075_cnblogs.cpp Normal file
View File

@ -0,0 +1,142 @@
#include <iostream>
#include <string>
#include <map>
using namespace std;
struct Node
{
struct Node *child[26];
int perfixNum;
bool isWord;
char ansChar[111];
};
Node *root, *pNode;
void Init()
{
root = new Node;
for (int i = 0; i < 26; i++)
{
root->child[i] = NULL;
}
}
void Insert(char word[], char ansChar[])
{
int len = strlen(word);
Node *pNode = root;
for (int i = 0; i < len; i++)
{
if (pNode->child[word[i] - 'a'] == NULL)
{
Node *newNode = new Node;
newNode->perfixNum = 1;
newNode->isWord = 0;
for (int j = 0; j < 26; j++)
{
newNode->child[j] = NULL;
}
pNode->child[word[i] - 'a'] = newNode;
}
else
{
pNode->child[word[i] - 'a']->perfixNum++;
}
pNode = pNode->child[word[i] - 'a'];
if(i == len - 1)
{
pNode->isWord = 1;
strcpy(pNode->ansChar, ansChar);
}
}
}
bool Find(char word[])
{
int len = strlen(word);
pNode = root;
int i;
for (i = 0; i < len; i++)
{
if (pNode->child[word[i] - 'a'] != NULL)
{
pNode = pNode->child[word[i] - 'a'];
}
else
{
break;
}
}
//字典中存在该单词
if (i == len && pNode->isWord == 1)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
char getChar[11];
gets(getChar);
char str1[33], str2[33];
Init();
while (scanf("%s%s", str1, str2), str1[0] != 'E')
{
Insert(str2, str1);//插入,单词,单词的翻译
}
getchar();
char ansStr[3333];
char saveStr[33];
while (gets(ansStr), ansStr[0] != 'E')
{
int i;
int k = 0;
for (i = 0; i < strlen(ansStr); i++)
{
if (ansStr[i] < 'a' || ansStr[i] > 'z')
{
if (ansStr[i - 1] >= 'a' && ansStr[i - 1] <= 'z')
{
saveStr[k] = '\0';
k = 0;
if (Find(saveStr))
{
printf("%s", pNode->ansChar);//如果字典中存在该单词,则输出它的翻译
}
else
{
printf("%s", saveStr);//字典中不存在该单词,输出原单词
}
}
printf("%c", ansStr[i]);
}
else
{
saveStr[k++] = ansStr[i];//保存单词
}
}
puts("");
}
return 0;
}

28
HDOJ/1284_cnblogs.cpp Normal file
View File

@ -0,0 +1,28 @@
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
const int M = 32768 + 10;
int dp[M];
int main()
{
int n;
while (~scanf("%d", &n))
{
memset(dp, 0, sizeof(dp));
dp[0] = 1;
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= n; j++)
{
dp[j] += dp[j - i];
}
}
printf("%d\n", dp[n]);
}
return 0;
}

32
HDOJ/1398_cnblogs.cpp Normal file
View File

@ -0,0 +1,32 @@
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
const int M = 300 + 10;
int dp[M];
int main()
{
int n;
while (~scanf("%d", &n), n)
{
memset(dp, 0, sizeof(dp));
dp[0] = 1;
int max = (int)sqrt(n * 1.0);
for (int i = 1; i <= max; i++)
{
for (int j = i * i; j <= n; j++)
{
dp[j] += dp[j - i * i];
}
}
printf("%d\n", dp[n]);
}
return 0;
}

44
HDOJ/2069_cnblogs.cpp Normal file
View File

@ -0,0 +1,44 @@
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std;
const int M = 300 + 10;
int dp[111][M];
int c[] = {0, 1, 5, 10, 25, 50};
int main()
{
//freopen("in.txt", "r", stdin);
int n;
while (~scanf("%d", &n))
{
memset(dp, 0, sizeof(dp));
dp[0][0] = 1;
for (int i = 1; i <= 5; i++)//枚举硬币总类
{
for (int num = 1; num <= 100; num++)//枚举硬币个数
{
for (int j = c[i]; j <= n; j++)//枚举容量
{
dp[num][j] += dp[num - 1][j - c[i]];
}
}
}
int ans = 0;
for (int i = 0; i <= 100; i++)//累加答案
{
ans += dp[i][n];
}
printf("%d\n", ans);
}
return 0;
}

View File

@ -0,0 +1,57 @@
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int VMAXN = 2e4 + 10;
const int TMAXN = 100 + 10;
const int INF = 0x3f3f3f3f;
int num[TMAXN], dp[VMAXN], k, n, m;
int ans[TMAXN];
bool Check(int sum)
{
int &cur = dp[sum];
if (cur != -1)
return cur;
if (sum == 0)
return cur = 1;
for (int i = 0; i < k; i++)
if (sum >= ans[i] && Check(sum - ans[i]))
return cur = 1;
return cur = 0;
}
void DFSID(int cur, int dep)
{
if (dep == k)
{
memset(dp, -1, sizeof dp);
if (Check(n))
{
printf("%d", k);
for (int i = 0; i < dep; i++)
printf(" %d", ans[i]);
puts("");
exit(0);
}
return;
}
if (cur >= m)
return;
ans[dep] = num[cur];
DFSID(cur + 1, dep + 1);
DFSID(cur + 1, dep);
}
int main()
{
//freopen("input.txt", "r", stdin);
int i, j;
scanf("%d%d", &n, &m);
for (i = 0; i < m; i++)
scanf("%d", &num[i]);
sort(num, num + m);
for (k = 1; k <= m; k++)
DFSID(0, 0);
return 0;
}

View File

@ -0,0 +1,57 @@
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int VMAXN = 2e4 + 10;
const int TMAXN = 100 + 10;
const int INF = 0x3f3f3f3f;
int num[TMAXN], dp[VMAXN], k, n, m;
int ans[TMAXN];
bool Check(int sum)
{
int &cur = dp[sum];
if (cur != -1)
return cur;
if (sum == 0)
return cur = 1;
for (int i = 0; i < k; i++)
if (sum >= ans[i] && Check(sum - ans[i]))
return cur = 1;
return cur = 0;
}
void DFSID(int cur, int dep)
{
if (dep == k)
{
memset(dp, -1, sizeof dp);
if (Check(n))
{
printf("%d", k);
for (int i = 0; i < dep; i++)
printf(" %d", ans[i]);
puts("");
exit(0);
}
return;
}
if (cur >= m)
return;
ans[dep] = num[cur];
DFSID(cur + 1, dep + 1);
DFSID(cur + 1, dep);
}
int main()
{
//freopen("input.txt", "r", stdin);
int i, j;
scanf("%d%d", &n, &m);
for (i = 0; i < m; i++)
scanf("%d", &num[i]);
sort(num, num + m);
for (k = 1; k <= m; k++)
DFSID(0, 0);
return 0;
}