第八届程序设计竞赛-题目分析
2013-10-09 18:44
218 查看
出题人:lrj
下面给出部分题目的解答:
题目A. 三家人
有点像脑筋急转弯!
这题因为题目中说了一定存在空字典,这里wa了2次。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <stack>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
#define LL long long
#define M 105
#define DEBUG puts("It's here!")
#define INF 1<<29
#define kind 30
#define CLS(x,v) memset(x,v,sizeof(x))
#define FOR(i,a,n) for(int i=(a);i<=(n);++i)
map<string,string> m;
set<string> add,del,update;
char a[102];
void init()
{
m.clear();
add.clear();
del.clear();
update.clear();
}
void split()
{
int i=1;
int len=strlen(a)-1;//去除右括号
string key,val;
while(i<len)
{
key="";val="";
while(i<len&&a[i]!=':'){ key+=a[i];i++;}
i++;
while(i<len&&a[i]!=','){ val+=a[i];i++;}
i++;
if(key.length()>0)
m.insert(make_pair<string, string>(key, val));
}
}
void solve()
{
int i=1;
int len=strlen(a)-1;//去除右括号
string key,val;
map<string,string>::iterator it;
while(i<len)
{
key="";val="";
while(i<len&&a[i]!=':'){ key+=a[i];i++;}
i++;
while(i<len&&a[i]!=','){ val+=a[i];i++;}
i++;
if(key.length()>0)
{
it=m.find(key);
if(it==m.end())
{ // 原来的字典中不存在
add.insert(key);
}
else //原来的字典中存在
{
if(it->second!=val) //value改变
update.insert(key);
m.erase(it);//del
}
}
}
}
void show(set<string>& out)
{
set<string>::iterator it=out.begin();
printf("%s",(*it).c_str());
for(++it;it!=out.end();++it)
printf(",%s",(*it).c_str());
printf("\n");
}
void output()
{
int flag=1;
if(add.size()>0){flag=0;printf("+");show(add);}
if(del.size()>0){flag=0;printf("-");show(del);}
if(update.size()>0){flag=0;printf("*");show(update);}
if(flag)printf("No changes\n");
printf("\n");
}
int main()
{
int T;
//freopen("in.txt","r",stdin);
scanf("%d",&T);
while(T--)
{
init();
scanf("%s",a);
split();
scanf("%s",a);
solve();
map<string,string>::iterator it;
for(it=m.begin(); it!=m.end(); it++)
del.insert(it->first);
output();
}
return 0;
}
题目E. 最短的名字
在一个奇怪的村子中,很多人的名字都很长,比如aaaaa, bbb and abababab。
名字这么长,叫全名显然起来很不方便。所以村民之间一般只叫名字的前缀。比如叫'aaaaa'的
时候可以只叫'aaa',因为没有第二个人名字的前三个字母是'aaa'。不过你不能叫'a',因为有两
个人的名字都以'a'开头。村里的人都很聪明,他们总是用最短的称呼叫人。输入保证村里不会
有一个人的名字是另外一个人名字的前缀(作为推论,任意两个人的名字都不会相同)。
如果村里的某个人要叫所有人的名字(包括他自己),他一共会说多少个字母?
输入格式
输入第一行为数据组数T (T<=10)。每组数据第一行为一个整数n(1<=n<=1000),即村里的人
数。以下n 行每行为一个人的名字(仅有小写字母组成)。输入保证一个村里所有人名字的长
度之和不超过1,000,000。
输出格式
对于每组数据,输出所有人名字的字母总数。
样例输入 样例输出
1 5
3
aaaaa
bbb
abababab
The Eighth Hunan Collegiate Programming Contest HN第八届大学生程序设计竞赛 2012 /10/12 AM9:00-14:00 考点 难度 题目A. 三家人 计算题 ☆ 题目B. 机器人的指令 简单模拟 ☆ Problem C. Updating a Dictionary 字符串处理 ☆☆ 题目D. 平方根大搜索 二分求开方+高精度 题目E. 最短的名字 trie 树 统计字符 ☆☆ Problem F. Kingdoms 题目G. 网格中的三角形 计算几何 ☆☆ Problem H. Tin Cutter II Problem I. Collecting Coins 题目J. 病毒 LCIS ☆☆ Problem K. Cross-Shaped Tests 题目L. 安全区域 3D计算几何 |
题目A. 三家人
有点像脑筋急转弯!
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int main() { int T; cin>>T; double a,b,money; while(T--) { cin>>a>>b>>money; double single=(a+b)/3;//每个人自己的那份 double ans=money*(a-single)/single; printf("%.0lf\n",ans);//注意四舍五入 } return 0; }题目B. 机器人的指令
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int main() { int i,j,n,T; cin>>T; char s[100]; int a[102]; int pos; while(T--) { pos=0; scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%s",s); if(s[0]=='L') { pos--; a[i]=-1; } else if(s[0]=='R') { pos++; a[i]=1; } else { scanf("%s",s); scanf("%d",&j); a[i]=a[j]; pos+=a[i]; } } printf("%d\n",pos); } return 0; }Problem C. Updating a Dictionary
这题因为题目中说了一定存在空字典,这里wa了2次。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <stack>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
#define LL long long
#define M 105
#define DEBUG puts("It's here!")
#define INF 1<<29
#define kind 30
#define CLS(x,v) memset(x,v,sizeof(x))
#define FOR(i,a,n) for(int i=(a);i<=(n);++i)
map<string,string> m;
set<string> add,del,update;
char a[102];
void init()
{
m.clear();
add.clear();
del.clear();
update.clear();
}
void split()
{
int i=1;
int len=strlen(a)-1;//去除右括号
string key,val;
while(i<len)
{
key="";val="";
while(i<len&&a[i]!=':'){ key+=a[i];i++;}
i++;
while(i<len&&a[i]!=','){ val+=a[i];i++;}
i++;
if(key.length()>0)
m.insert(make_pair<string, string>(key, val));
}
}
void solve()
{
int i=1;
int len=strlen(a)-1;//去除右括号
string key,val;
map<string,string>::iterator it;
while(i<len)
{
key="";val="";
while(i<len&&a[i]!=':'){ key+=a[i];i++;}
i++;
while(i<len&&a[i]!=','){ val+=a[i];i++;}
i++;
if(key.length()>0)
{
it=m.find(key);
if(it==m.end())
{ // 原来的字典中不存在
add.insert(key);
}
else //原来的字典中存在
{
if(it->second!=val) //value改变
update.insert(key);
m.erase(it);//del
}
}
}
}
void show(set<string>& out)
{
set<string>::iterator it=out.begin();
printf("%s",(*it).c_str());
for(++it;it!=out.end();++it)
printf(",%s",(*it).c_str());
printf("\n");
}
void output()
{
int flag=1;
if(add.size()>0){flag=0;printf("+");show(add);}
if(del.size()>0){flag=0;printf("-");show(del);}
if(update.size()>0){flag=0;printf("*");show(update);}
if(flag)printf("No changes\n");
printf("\n");
}
int main()
{
int T;
//freopen("in.txt","r",stdin);
scanf("%d",&T);
while(T--)
{
init();
scanf("%s",a);
split();
scanf("%s",a);
solve();
map<string,string>::iterator it;
for(it=m.begin(); it!=m.end(); it++)
del.insert(it->first);
output();
}
return 0;
}
题目E. 最短的名字
在一个奇怪的村子中,很多人的名字都很长,比如aaaaa, bbb and abababab。
名字这么长,叫全名显然起来很不方便。所以村民之间一般只叫名字的前缀。比如叫'aaaaa'的
时候可以只叫'aaa',因为没有第二个人名字的前三个字母是'aaa'。不过你不能叫'a',因为有两
个人的名字都以'a'开头。村里的人都很聪明,他们总是用最短的称呼叫人。输入保证村里不会
有一个人的名字是另外一个人名字的前缀(作为推论,任意两个人的名字都不会相同)。
如果村里的某个人要叫所有人的名字(包括他自己),他一共会说多少个字母?
输入格式
输入第一行为数据组数T (T<=10)。每组数据第一行为一个整数n(1<=n<=1000),即村里的人
数。以下n 行每行为一个人的名字(仅有小写字母组成)。输入保证一个村里所有人名字的长
度之和不超过1,000,000。
输出格式
对于每组数据,输出所有人名字的字母总数。
样例输入 样例输出
1 5
3
aaaaa
bbb
abababab
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define mem(a,b) memset(a,b,sizeof(a)) #define INF 1<<30 #define M 1002 #define kind 26 //ascII个数 struct node{ int isword; node * next[kind]; node() { isword=0; mem(next,0); } }*root; int total;//输出的答案 char s[10002];// //构建字典树 void Insert(node *p,char s[]) { int index,i=0; while(s[i]) { index=s[i]-'a'; if(p->next[index]==NULL) p->next[index]=new node; p=p->next[index]; p->isword++; i++; } } void mycount(node *p) { for(int i=0;i<kind;i++) if(p->next[i]!=NULL) { if(p->next[i]->isword>1) total+=p->next[i]->isword; mycount(p->next[i]); } } //释放这棵字典树 void myfree(node *p) { for(int i=0;i<kind;i++) if(p->next[i]!=NULL) myfree(p->next[i]); delete p; } int main() { int n,T; cin>>T; while(T--) { scanf("%d",&n); root=new node; total=n; for(int i=1;i<=n;i++) { scanf("%s",s); Insert(root,s); } mycount(root); printf("%d\n",total); myfree(root); } return 0; }
相关文章推荐
- HDU 4484 Hailstone HOTPO (简单模拟题)
- linux 流量监控 ---iptraf的安装及使用
- linux 流量监控 ---iptraf的安装及使用
- java使用batik转换svg文件
- chromium aura简介
- 黑马程序员_高新技术_代理
- 遍历链表时常犯的一个错误
- SPI---小V的总结
- 关于GCC的理解——On the understanding of the GCC
- 算法小题目小结。。。
- DNSMASQ配置详解
- 快速加载properties文件
- [置顶] 斗地主算法的设计与实现--项目介绍&如何定义和构造一张牌
- linux系统加固paper
- WSGI的理解(转载)
- java中的闭包和回调
- 委托的一个最简单的例子
- 微分方程——基本概念和常微分方程的发展史
- [置顶] shell变量赋值-linux
- 高级IO复用应用:聊天室程序