目录
1130 Infix Expression (25分) (dfs)
1131 Subway Map (30分) (dfs)(新 ※)
1132 Cut Integer (20分) (水题)
1133 Splitting A Linked List (25分) (链表 cmp sort)待改 22
1134 Vertex Cover (25分) (水题 unordered_set st;)
1137 Final Grading (25分) (map,sort,cmp)待改 23
1138 Postorder Traversal (25分) (已知前序+中序 求后序)
1139 First Contact (30分) (水题)
1140 Look-and-say Sequence (20分)
1141 PAT Ranking of Institutions (25分) (结构体 sort) 待改 22
1142 Maximal Clique (25分) (无向图, (最大)完全子图)
1144 The Missing Number (20分) (E 水题)
1145 Hashing - Average Search Time (25分) (平方探测 hash)
1146 Topological Order (25分) (拓扑序)
1148 Werewolf - Simple Version (20分)
1149 Dangerous Goods Packaging (25分)(map
1150 Travelling Salesman Problem (25分) (旅行商问题=哈密顿回路)
#include
#include
#include
#include
#include
#include
#include
#define pb push_back
using namespace std;
const int maxn=100005;
int n,cnt[25];
struct node{
string data;
int left,right;
node(){
left=-1;right=-1;
}
};
vector g(25);
vector ans;
string dfs(int u){
if(g[u].left==-1 && g[u].right==-1) return g[u].data;
else if(g[u].left==-1 && g[u].right!=-1) return "("+g[u].data+dfs(g[u].right)+")";
else if(g[u].left!=-1 && g[u].right==-1) return "("+dfs(g[u].left)+g[u].data+")";
else if(g[u].left!=-1 && g[u].right!=-1) return "("+dfs(g[u].left)+g[u].data+dfs(g[u].right)+")";
}
int main(){
cin>>n;
string c; int a,b;
for(int i=1;i<=n;i++){
cin>>c>>a>>b;
g[i].data=c; g[i].left=a; g[i].right=b;
cnt[a]=1; cnt[b]=1;
}
int i=1;
for(;i<=n;i++)
if(cnt[i]==0) break;
int root=i;
string ans=dfs(root);
if(ans[0]=='(') ans=ans.substr(1,ans.size()-2);
cout<
#include
#include
#include
#include
#include
#include
#include
#define pb push_back
#define ll long long
using namespace std;
const int maxn=100005;
int n;
ll getnum(ll tmp){
//位数
ll cnt=0;
while(tmp!=0){
tmp/=10;
cnt++;
}
return cnt;
}
ll power(ll a,ll b){
ll tmp=1;
while(b--){
tmp*=a;
}
return tmp;
}
int main(){
cin>>n;
ll num,w,a,b,p1;
for(int i=0;i>num;
w=getnum(num);
p1=power(10,w/2);
a=num%p1; b=num/p1;
if((a*b)!=0&&num%(a*b)==0)cout<<"Yes\n";
else cout<<"No\n";
}
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#define pb push_back
using namespace std;
const int maxn=100005;
int start,n,k;
struct node{
int data;
int flag,index;
int addr,next;
node(){
flag=maxn;
index=maxn;
}
}num[maxn];
bool cmp(node a,node b){
if(a.flag!=b.flag)
return a.flagk){
num[add].flag=1;
}else{
num[add].flag=0;
}
}
int in=0;
while(start!=-1){
num[start].index=in++;
start=num[start].next;
}
sort(num,num+maxn,cmp);
for(int i=0;i
#include
#include
#include
#include
he/she must first obtain no less than 200 points from the online programming assignments, and then receive a final grade no less than 60 out of 100.
注意: rounded up to an integer 四舍五入为整数
#include
#include
#include
#include
#include
#include
#include
#include
vector
给定n<50000 ,实际开50 0005数组
#include
#include
#include
#include
#include
#include
#include
#define pb push_back
using namespace std;
const int maxn=500005;
int n,pre[maxn],in[maxn],pos[maxn];
vector post;
bool flag;
void dfs(int prel,int prer,int inl,int inr){
if(inl>inr || flag) return ;
int head=pos[pre[prel]];
int len=head-inl;
dfs(prel+1,prel+len,inl,head-1);
dfs(prel+len+1,prer,head+1,inr);
if(!flag){
flag=true;
cout<
1139 First Contact (30分) (水题)
1140 Look-and-say Sequence (20分)
#include
#include
#include
using namespace std;
int main() {
string s;
int n, j;
cin >> s >> n;
for (int cnt = 1; cnt < n; cnt++) {
string t;
for (int i = 0; i < s.length(); i = j) {
for (j = i; j < s.length() && s[j] == s[i]; j++);
t += s[i] + to_string(j - i);
}
s = t;
}
cout << s;
return 0;
}
1141 PAT Ranking of Institutions (25分) (结构体 sort)
22
#include
#include
#include
#include
#include
#include
#include
1142 Maximal Clique (25分) (无向图, (最大)完全子图)
分析:先判断是否是clique,即判断是否任意两边都相连;之后判断是否是maximal,即遍历所有不在集合中的剩余的点,看是否存在⼀个点满⾜和集合中所有的结点相连,最后如果都满⾜,那就输出Yes表示是Maximal clique
#include
#include
using namespace std;
int e[210][210];
int main() {
int nv, ne, m, ta, tb, k;
scanf("%d %d", &nv, &ne);
for (int i = 0; i < ne; i++) {
scanf("%d %d", &ta, &tb);
e[ta][tb] = e[tb][ta] = 1;
}
scanf("%d", &m);
for (int i = 0; i < m; i++) {
scanf("%d", &k);
vector v(k);
int hash[210] = {0}, isclique = 1, isMaximal = 1;
for (int j = 0; j < k; j++) {
scanf("%d", &v[j]);
hash[v[j]] = 1;
}
for (int j = 0; j < k; j++) {
if (isclique == 0) break;
for (int l = j + 1; l < k; l++) {
if (e[v[j]][v[l]] == 0) {
isclique = 0;
printf("Not a Clique\n");
break;
}
}
}
if (isclique == 0) continue;
for (int j = 1; j <= nv; j++) {
if (hash[j] == 0) {
for (int l = 0; l < k; l++) {
if (e[v[l]][j] == 0) break;
if (l == k - 1) isMaximal = 0;
}
}
if (isMaximal == 0) {
printf("Not Maximal\n");
break;
}
}
if (isMaximal == 1) printf("Yes\n");
}
return 0;
}
1144 The Missing Number (20分) (E 水题)
#include
#include
#include
#include
#include
#include
#include
#define pb push_back
using namespace std;
const int maxn=100005;
int n;
int Hash[maxn];
int main(){
scanf("%d",&n);
int tmp;
for(int i=0;i=0 && tmp
1145 Hashing - Average Search Time (25分) (平方探测 hash)
类似: 1078 Quadratic probing:平方探测
#include
#include
#include
using namespace std;
bool isprime(int n) {
for (int i = 2; i * i <= n; i++)
if (n % i == 0) return false;
return true;
}
int main() {
int tsize, n, m, a;
scanf("%d %d %d", &tsize, &n, &m);
while(!isprime(tsize)) tsize++;
vector v(tsize);
for (int i = 0; i < n; i++) {
scanf("%d", &a);
int flag = 0;
for (int j = 0; j < tsize; j++) {
int pos = (a + j * j) % tsize;
if (v[pos] == 0) {
v[pos] = a;
flag = 1;
break;
}
}
if (!flag) printf("%d cannot be inserted.\n", a);
}
int ans = 0;
for (int i = 0; i < m; i++) {
scanf("%d", &a);
for (int j = 0; j <= tsize; j++) {
ans++;
int pos = (a + j * j) % tsize;
if (v[pos] == a || v[pos] == 0) break;
}
}
printf("%.1f\n", ans * 1.0 / m);
return 0;
}
1146 Topological Order (25分) (拓扑序)
给定一个DAG,和几个序列,判断这些序列是否是DAG的拓扑序
(注:单纯求拓扑序可以用dfs,或是bfs(每次让度=0的点入队))
分析:⽤邻接表v存储这个有向图,并将每个节点的⼊度保存在in数组中。
对每⼀个要判断是否是拓扑序列的结点遍历,如果当前结点的⼊度不为0则表示不是拓扑序列,每次选中某个点后要将它所指向的所有结点的⼊度-1,最后根据是否出现过⼊度不为0的点决定是否要输出当前的编号i~
flag是⽤来判断之前是否输出过现在是否要输出空格的~judge是⽤来判断是否是拓扑序列的
#include
#include
using namespace std;
int main() {
int n, m, a, b, k, flag = 0, in[1010];
vector v[1010];
scanf("%d %d", &n, &m);
for (int i = 0; i < m; i++) {
scanf("%d %d", &a, &b);
v[a].push_back(b);
in[b]++;
}
scanf("%d", &k);
for (int i = 0; i < k; i++) {
int judge = 1;
vector tin(in, in+n+1);
for (int j = 0; j < n; j++) {
scanf("%d", &a);
if (tin[a] != 0) judge = 0;
for (int it : v[a]) tin[it]--;
}
if (judge == 1) continue;
printf("%s%d", flag == 1 ? " ": "", i);
flag = 1;
}
return 0;
}
1148 Werewolf - Simple Version (20分)
分析:每个⼈说的数字保存在v数组中,i从1~n、j从i+1~n遍历,分别假设i和j是狼⼈,a数组表示该⼈是狼⼈还是好⼈,等于1表示是好⼈,等于-1表示是狼⼈。
k从1~n分别判断k所说的话是真是假,k说的话和真实情况不同(即v[k] * a[abs(v[k])] < 0)则表示k在说谎,则将k放在lie数组中;遍历完成后 判断lie数组,如果说谎⼈数等于2并且这两个说谎的⼈⼀个是好⼈⼀个是狼⼈(即a[lie[0]] + a[lie[1]] == 0)表示满⾜题意,此时输出i和j并return,否则最后的时候输出No Solution
#include
#include
#include
using namespace std;
int main() {
int n;
cin >> n;
vector v(n+1);
for (int i = 1; i <= n; i++) cin >> v[i];
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
vector lie, a(n + 1, 1);
a[i] = a[j] = -1;
for (int k = 1; k <= n; k++)
if (v[k] * a[abs(v[k])] < 0) lie.push_back(k);
if (lie.size() == 2 && a[lie[0]] + a[lie[1]] == 0) {
cout << i << " " << j;
return 0;
}
}
}
cout << "No Solution";
return 0;
}
1149 Dangerous Goods Packaging (25分)(map> )
#include
#include
#include
#include
1150 Travelling Salesman Problem (25分) (旅行商问题=哈密顿回路)
Given a list of cities and the distances between each pair of cities, what is the shortest possible route that visits each city and returns to the origin city
跟之前那道哈密顿的题差不多,
#include
#include
#include
#include
#include
#include
#include
#include
#define pb push_back
using namespace std;
const int maxn=100005;
int n,m,k;
int g[205][205],path[205];
int main(){
cin>>n>>m;
int a,b,d;
for(int i=0;i>a>>b>>d;
g[a][b]=g[b][a]=d;
}
int num,flag,sum,minsum=0x3f3f3f3f,index;
cin>>k;
for(int i=1;i<=k;i++){
set st;
flag=1;
sum=0;
memset(path,0,sizeof(path));
cin>>num;
cout<<"Path "<>path[j];
st.insert(path[j]);
if(j>0 && g[path[j-1]][path[j]]==0){
flag=0;
}else{
sum+=g[path[j-1]][path[j]];
}
}
if( st.size()!=n || path[0]!=path[num-1] || !flag){
if(flag==1){
cout<