Volume 1. Sorting/Searching(uva)

时间:2022-01-21 00:49:26

340 - Master-Mind Hints

/*读了老半天才把题读懂,读懂了题输出格式没注意,结果re了两次。

题意:先给一串数字S,然后每次给出对应相同数目的的一串数字Si,然后优先统计Si和S对应位相同并且相等的个数L,在统计不在对应位上但相等的的个数R.

当Si全0时,表示结束。

每个数只能用一次。

例如:有

S  1 3 5 5
S1 1 1 2 3
S2 4 3 3 5
Si . . . .
0 0 0 0 对于S1:则第一步只有第一个对应相等,所以L = 1
就还剩下3 5 5
    1 2 3,在统计R,下面只有3和上面的3相同,所以R = 1;
对于S2:L = 2,第二个位置和第四个位置对应相等。
就还剩下1 5
4 3,所以R = 0;依次类推。
*/
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#define N 1005
using namespace std; int n;
int a[N], b[N], c[N];
int main()
{
int i, j, t = , l, r;
while (cin>>n, n)
{
cout<<"Game "<<t++<<':'<<endl;
for (i = ; i < n; i++)
cin>>a[i];
while (true)
{
for (i = ; i < n; i++)
c[i] = a[i];
l = r = ;
for (i = ; i < n; i++)
{
cin>>b[i];
if (b[i] == c[i])/**< 统计对应位相等l的个数 */
{
l++;
c[i] = ;/**< 置相等位0,统计r时不影响 */
b[i] = -;
}
}
if (!b[])
break;
for (i = ; i < n; i++)
{
if (b[i] == -)
continue;
for (j = ; j < n; j++)
{
if (b[i] == c[j])/**< 统计r的个数 */
{
r++;
c[j] = ;
break;
}
}
}
cout<<" "<<'('<<l<<','<<r<<')'<<endl;
}
}
return ;
}

10420 - List of Conquests

 /*题意:统计每个国家有多少个人被Giovanni喜欢。

 并按字典序输出。
*/ #include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <climits>
#define N 2005
using namespace std;
struct node
{
string c;
int sum;
node()
{
c.clear();
sum = ;
}
bool operator >= (const node &b)
{
return c >= b.c;
}
friend istream& operator >> (istream &in, node &res)
{
in>>res.c;
return in;
}
friend ostream &operator << (ostream &out, const node &res)
{
out<<res.c<<' '<<res.sum<<endl;
return out;
}
bool operator == (const node &b)
{
return c == b.c;
} bool operator != (const node &b)
{
return c != b.c;
}
}; struct li
{
node s[N];
int p;
li()
{
p = ;
}
friend ostream &operator << (ostream &out, const li &res)
{
for (int i = ; i < res.p; i++)
out<<res.s[i];
}
void insert(const node &res)
{
if (p)
{
int l = , r = p - , mid;
while (l <= r)/**< 二分查找第一个比res大或等于的位置 */
{
mid = (l + r) >> ;
if (s[mid] >= res)/**< 如果大于等于,在左区间位置 */
r = mid - ;
else/**< 在右区间 */
l = mid + ;
}
if (s[l] != res)/**< 不存在,插入 */
{
for (r = p++; r > l; r--)
s[r] = s[r - ];
s[l] = res;
}
s[l].sum++;
return;
}
s[p] = res;
s[p++].sum++;
}
};
li a;
int main()
{
int n, i;
cin>>n;
string tt;
node t;
for (i = ; i < n; i++)
{
cin>>t;
getline(cin, tt);
a.insert(t);
}
cout<<a;
return ;
}

10474 - Where is the Marble?

/**< 题意:给一组数据无序,查询给定数在这组数出现的第一个位置(在有序中) */
#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <climits>
#define N 10005
using namespace std;
int n, q; int sum[N], con[N];
int main()
{
int t = , i, m;
while (cin>>n>>q, n || q)
{
memset(con, , sizeof(con));
cout<<"CASE# "<<t++<<':'<<endl;
for (i = ; i < n; i++)
{
cin>>m;
con[m]++;/**< 计数排序,每个数不超过10000 */
}
sum[] = ;
sum[] = con[];
for (i = ; i < N; i++)
sum[i] = sum[i - ] + con[i - ];
for (i = ; i < q; i++)
{
cin>>m;
if (con[m])
cout<<m<<" found at "<<sum[m] + <<endl;
else
cout<<m<<" not found"<<endl;
}
}
return ;
}

152 - Tree's a Crowd

/**< 题意:(又是读了半天也没读懂,好纠结的英语)
在三维空间给一些点,计算每个点与其他点的距离,但只取最短距离,并统计最短距离在[0-10)的个数
比如某个点的最短距离在[1,2)之间,则对1这个位置的个数加1 */
#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <climits>
#include <cmath>
#include <algorithm>
#include<iomanip>
#define N 5005 using namespace std;
struct node
{
int x, y, z;
/*
bool operator > (const node &b)
{
if (x == b.x)
{
if (y == b.y)
return z > b.z;
return y > b.y;
}
return x > b.x;
}*/
}a[N];
int con[];
int dis(const node &a, const node &b)
{
int x = a.x - b.x, y = a.y - b.y, z = a.z - b.z;
return int(sqrt(x * x + y * y + z * z));
} int cmp(const void *a, const void *b)/**< */
{
return ((node *)a)->x > ((node *)b)->x ? : -;
}
int main()
{
int n = , i, j, tm, t;
while (cin>>a[n].x>>a[n].y>>a[n].z, (a[n].x || a[n].y || a[n].z))
{
n++;
}
//qsort(a, n, sizeof(a[0]), cmp);
memset(con, , sizeof(con));
for (i = ; i < n; i++)
{
tm = ;
for (j = ; j < n; j++)
{
if (i != j)
{
t = dis(a[i], a[j]);
if (t < tm)
tm = t;
}
}
con[tm]++;
}
for (i = ; i < ; i++)
cout<<setw()<<con[i];
cout<<endl;
return ;
}

299 - Train Swapping

/**<
题目啰嗦了半天,就是火车进行从小到大重排序(冒泡排序)交换的次数
*/
#include <iostream>
#include <algorithm> using namespace std;
#define N 55
int a[N];
int main()
{
int n, l, i, j, s;
cin>>n;
while (n--)
{
cin>>l;
for (i = ; i < l; i++)
cin>>a[i];
s = ;
for (i = ; i < l; i++)
for (j = l - ; j > i; j--)
{
if (a[j] < a[j - ])
{
swap(a[j], a[j - ]);
s++;
}
}
cout<<"Optimal train swapping takes "<<s<<" swaps."<<endl;
}
return ;
}

120 - Stacks of Flapjacks

/**<  翻转从小到大排序*/
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
#define N 35 int a[N];
int main()
{
int i, j, n = , p, mm, t;
char ch = ;
while (ch != EOF && cin>>a[n])
{
ch = cin.get();
if (ch == ' ')
{
n++;
continue;
}
t = ++n;
cout<<a[];
for (i = ; i < n; i++)
cout<<' '<<a[i];
cout<<endl;
for (i = ; i < n; n--)
{
mm = a[i];
p = ;
for (j = ; j < n; j++)
{
if (mm < a[j])
{
mm = a[j];
p = j;
}
}
if (p != n - )
{
if (p)
{
reverse(a, a + p + );
reverse(a, a + n);
cout<<t - p<<' '<<t - n + <<' ';
}
else
{
reverse(a, a + n);
cout<<t - n + <<' ';
}
}
}
cout<<<<endl;
}
return ;
}

156 - Ananagrams

/**<
给一些单词,去掉相同字母且个数一样不区分大小写,再给剩下的单词进行排序输出
*/
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#define N 1005
using namespace std;
struct node
{
string str;
bool flag;
int tag[];
bool operator == (const node &b)
{
for (int i = ; i < ; i++)
if (tag[i] != b.tag[i])
return false;
return true;
}
bool operator > (const node &b)
{
return str > b.str;
}
node(const string &s)
{
str = s;
flag = false;
memset(tag, , sizeof(tag));
for (int i = ; s[i]; i++)
tag[(s[i] | ) - 'a']++;
}
node()
{
flag = false;
memset(tag, , sizeof(tag));
}
}s[N];
int p = ; void insert(const string &v)
{
if (!p)
{
s[p++] = v;
return;
}
for (int i = ; i < p; i++)
if (s[i] == v)
{
s[i].flag = true;
return;
}
int l = , r = p - , mid;
while (l <= r)
{
mid = (l + r) >> ;
if (s[mid] > v)
r = mid - ;
else
l = mid + ;
}
for (r = p++; r > l; r--)
s[r] = s[r - ];
s[l] = v;
} string t;
int main()
{
int i;
char ch;
cin.get(ch);
while (ch != '#')
{
t.clear();
while (ch == ' ' || ch == '\n')
cin.get(ch);
if (ch == '#')
break;
while (true)
{
t += ch;
cin.get(ch);
if (ch == ' ' || ch == '\n')
break;
}
insert(t);
}
for (i = ; i < p; i++)
if (!s[i].flag)
cout<<s[i].str<<endl;
return ;
}

400 - Unix ls

/**<
对输入的字符串进行排序,按格式输出,最后一列的宽度为所有字符串最长宽度,其余列的宽度为最长宽度加2.
要求输出所用的行最少
*/
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <iomanip> using namespace std;
#define N 105
string s[N];
int main()
{
int n, i, mm, len, j, t, k, kk;
while (cin>>n)
{
mm = ;
for (i = ; i < n; i++)
{
cin>>s[i];
len = s[i].length();
if (mm < len)
mm = len;
}
sort(s, s + n);
len = ( - mm) / (mm + ) + ;
for (i = ; i < ; i++)
cout<<'-';
cout<<endl;
cout<<left;
t = n / len;/**< 要输出t行len列 */
if (n % len)
t++;
for (i = ; i < t; i++)
{
kk = mm + ;
for (j = i, k = ; j < n; j += t, k++)
{
if (k == len)/**< 最后一列输出宽度为mm */
kk = mm;
cout<<setw(kk)<<s[j];
}
cout<<endl;
}
}
return ;
}

123 - Searching Quickly

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <iomanip>
#include <set> using namespace std; struct snode
{
string str;
int x, y;/**< 字符串位置 */
bool operator < (const snode &b)const
{
if (str == b.str)
{
if (x == b.x)
return y < b.y;
return x < b.x;
}
return str < b.str;
}
snode()
{
str.clear();
x = y = ;
}
snode(string v, int xx, int yy)
{
str = v;
x = xx;
y = yy;
}
friend ostream & operator << (ostream &out, const snode &v)
{
out<<v.str<<' '<<v.x<<' '<<v.y;
return out;
}
}; set<string> ks;
multiset<snode> res;
string ts[]; int main()
{
int i, j, p;
string tmp;
ks.clear();
res.clear();
while (cin>>tmp, tmp != "::")
{
ks.insert(tmp);
}
i = ;
cin.get();
while (getline(cin, ts[i]))
{
j = ;
while (true)
{
p = j;
tmp.clear();
while (ts[i][j] && ts[i][j] != ' ')
{
ts[i][j] |= ;
tmp += ts[i][j++];
}
if (ks.find(tmp) == ks.end())/**< 不存在 */
res.insert(snode(tmp, i, p));
if (!ts[i][j])
break;
j++;
}
i++;
}
for (multiset<snode>::iterator it = res.begin(); it != res.end(); it++)
{
for (i = ; i < it->y; i++)
cout<<ts[it->x][i];
for (; ts[it->x][i] != ' ' && ts[it->x][i]; i++)/**< 大写 */
cout<<char(ts[it->x][i] & );
for (; ts[it->x][i]; i++)
cout<<ts[it->x][i];
//cout<<*it;
cout<<endl;
}
return ;
}

10194 - Football (aka Soccer)

#include <iostream>
#include <cstdio>
#include <fstream>
#include <cstring>
#include <string>
#include <cctype>
#include <iomanip>
#include <cstdlib>
#include <algorithm>
#include <set>
#include <map> using namespace std; struct snode
{
snode()
{
score = in = out = win = con = tie = lose = ;
name.clear();
_name.clear();
}
snode(string v)
{
score = in = out = win = con = tie = lose = ;
name = v;
_name.clear();
for (int i = ; v[i]; i++)
{
if (isalnum(v[i]))
_name += (v[i] | );
else
_name += v[i];
}
}
string name;
string _name;
int score;/**< 总得分 */
int in;/**< 进球 */
int out;/**< 对方进球 */
int win;/**< 赢得场数 */
int con;/**< 参加比赛次数 */
int tie;/**< 平局次数 */
int lose;/**< 输的次数 */
bool operator < (const snode &b)const
{
if (score == b.score)
{
if (win == b.win)
{
int ta = in - out, tb = b.in - b.out;
if (ta == tb)
{
if (in == b.in)
{
if (con == b.con)
{
return _name < b._name;
}
return con < b.con;
}
return in > b.in;
}
return ta > tb;
}
return win > b.win;
}
return score > b.score;
}
friend istream &operator >> (istream &in, snode &v)
{
string tmp;
getline(in, tmp);
v = tmp;
return in;
} friend ostream &operator << (ostream &out, const snode &v)
{
cout<<v.name<<' '<<v.score<<"p, "<<v.con<<"g ("<<v.win<<'-'<<v.tie<<'-'<<v.lose<<"), ";
cout<<v.in - v.out<<"gd ("<<v.in<<'-'<<v.out<<')';
return out;
} bool operator == (const snode &v)
{
return name == v.name;
}
}a[];
int n, t, g; int find_str(string v)
{
for (int i = ; i < t; i++)
if (a[i] == v)
return i;
return -;
} void fun(string v)
{
int i = -;
while (v[++i] != '#');
string s1(v.begin(), v.begin() + i);
/**< 数字处 */
int t1 = v[++i] - '';
if (isdigit(v[++i]))/**< 第二个是不是数字 */
t1 = * t1 + (v[i++] - '');
int t2 = v[++i] - '';
if (isdigit(v[++i]))/**< 第二个是不是数字 */
t2 = * t2 + (v[i++] - '');
string s2(v.begin() + i + , v.end());
int p1 = find_str(s1), p2 = find_str(s2);
//cout<<p1<<p2<<endl;
if (t1 > t2)/**< 1赢 */
{
a[p1].win++;
a[p2].lose++;
a[p1].score += ;
}
else
if (t1 == t2)/**< 平 */
{
a[p1].tie++;
a[p2].tie++;
a[p1].score++;
a[p2].score++;
}
else/**< 2赢 */
{
a[p2].win++;
a[p1].lose++;
a[p2].score += ;
}
a[p1].con++;
a[p2].con++;
a[p1].in += t1;
a[p2].in += t2;
a[p1].out += t2;
a[p2].out += t1;
} int main()
{
int i, j;
cin>>n;
cin.get();
string st;
while (n--)
{
getline(cin, st);
cout<<st<<endl;
cin>>t;
cin.get();
for (i = ; i < t; i++)
cin>>a[i];
cin>>g;
cin.get();
for (i = ; i < g; i++)
{
getline(cin, st);
fun(st);
}
sort(a, a + t);
for (i = ; i < t; i++)
{
cout<<i + <<") "<<a[i]<<endl;
}
if (n)
cout<<endl;
}
return ;
}

755 - 487--3279

#include <iostream>
#include <cstdio>
#include <fstream>
#include <cstring>
#include <string>
#include <cctype>
#include <iomanip>
#include <cstdlib>
#include <algorithm>
#include <set>
#include <map> using namespace std;
int ls[] =
{
, , , , , , , , , ,
, , , , , , , , , ,
, , , , ,
};
char get(char ch)
{
return ls[ch - 'A'] + '';
} map<string, int> mp;
int main()
{
int t, n, i, tag;
cin>>t;
char ch;
string tmp;
while (t--)
{
mp.clear();
cin>>n;
cin.get();
for (i = ; i < n; i++)
{
tmp.clear();
while (ch = cin.get(), ch != '\n')/**< 对输入进行处理换成全数字的字符串 */
{
if (isdigit(ch))
{
tmp += ch;
continue;
}
if (isalpha(ch))
{
tmp += get(ch);
continue;
}
}
mp[tmp]++;/**< 对应字符串个数加1 */
}
tag = true;
for (map<string, int>::iterator it = mp.begin(); it != mp.end(); ++it)
{
if (it->second > )
{
tmp = it->first;
tmp.insert(tmp.begin() + , '-');
cout<<tmp<<' '<<it->second<<endl;
tag = false;
}
}
if (tag)
cout<<"No duplicates."<<endl;
if (t)
cout<<endl; }
return ;
}

10785 - The Mad Numerologist

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <iomanip>
#include <cctype>
#include <climits>
#include <algorithm>
#include <map>
#include <set> using namespace std;
map<char, int> mp1, mp2;
char s1[] =
{
'J', 'S', 'B', 'K', 'T', 'C', 'L', 'D', 'M', 'V',
'N', 'W', 'F', 'X', 'G', 'P', 'Y', 'H', 'Q', 'Z',
'R'
};
char s2[] =
{
'A' , 'U', 'E', 'O', 'I'
};
int n1, n2, p1, p2;
void fun1()
{
while (n1 > )
{
p1++;
if (n1 >= )
mp1[s1[p1]] = ;
else
mp1[s1[p1]] = n1;
n1 -= ;
}
}
void fun2()
{
while (n2 > )
{
p2++;
if (n2 >= )
mp2[s2[p2]] = ;
else
mp2[s2[p2]] = n2;
n2 -= ;
}
}
void print()
{
int i;
string s1, s2;
s1.clear();
s2.clear();
for (map<char, int>::iterator it = mp1.begin(); it != mp1.end(); ++it)
{
for (i = it->second; i > ; i--)
s1 += it->first;
}
for (map<char, int>::iterator it = mp2.begin(); it != mp2.end(); ++it)
{
for (i = it->second; i > ; i--)
s2 += it->first;
}
i = ;
while (s1[i] && s2[i])
{
cout<<s2[i]<<s1[i];
i++;
}
if (s2[i])
cout<<s2[i];
cout<<endl;
}
int main()
{
int m, n, t = ;
cin>>m;
while (m--)
{
cout<<"Case "<<t++<<": ";
cin>>n;
p1 = p2 = -;
n1 = n / ;
n2 = n1 + (n & );
mp1.clear();
mp2.clear();
fun1();
fun2();
print();
}
return ;
}

Volume 1. Sorting/Searching(uva)的更多相关文章

  1. Csharp&colon;Paging Sorting Searching In ASP&period;NET MVC 5

    http://www.c-sharpcorner.com/UploadFile/0c1bb2/sorting-paging-searching-in-Asp-Net-mvc-5/ https://dz ...

  2. 刘汝佳 算法竞赛-入门经典 第二部分 算法篇 第五章 3(Sorting&sol;Searching)

    第一题:340 - Master-Mind Hints UVA:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Item ...

  3. Volume 1&period; Big Number&lpar;uva&rpar;

    如用到bign类参见大整数加减乘除模板 424 - Integer Inquiry #include <iostream> #include <string> #include ...

  4. &lbrack;转&rsqb;Paging&comma; Searching and Sorting in ASP&period;Net MVC 5

    本文转自:http://www.c-sharpcorner.com/UploadFile/4b0136/perform-paging-searching-sorting-in-Asp-Net-mvc- ...

  5. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  6. The Art of Computer Programming

    <计算机程序设计艺术>即<The Art of Computer Programming>是计算机领域里颠峰级的里程碑,加上国外人士对它的推崇,所以提起它的大名简直就象法律书籍 ...

  7. Trie(前缀树&sol;字典树)及其应用

    Trie,又经常叫前缀树,字典树等等.它有很多变种,如后缀树,Radix Tree/Trie,PATRICIA tree,以及bitwise版本的crit-bit tree.当然很多名字的意义其实有交 ...

  8. pyspark 连 MongoDB复制集

    解决问题思路: 核心:0-理解pyspark的执行与java jar的关系: 1-看控制台,看日志: 2-jar缺不缺,版本号,放哪里. [root@hadoop1 mylocalRepository ...

  9. Linux Kernel中所應用的數據結構及演算法

    Linux Kernel中所應用的數據結構及演算法 Basic Data Structures and Algorithms in the Linux kernel Links are to the  ...

随机推荐

  1. Python之路 day3 高阶函数

    #!/usr/bin/env python # -*- coding:utf-8 -*- #Author:ersa """ 变量可以指向函数,函数的参数能接收变量, 那么 ...

  2. WebApi系列~QQ互联的引入&lpar;QConnectSDK&rpar;

    回到目录 感谢与改进 首先要感谢张善友老兄为大家封装的这个DLL,它将QQ官方的相关API都集成到了这个里面,这对于开发人员来说,是个福音,有人会说,为什么QQ官方没有提供.net版的SDK呢,在这里 ...

  3. Windows Azure Virtual Machine &lpar;27&rpar; 使用psping工具,测试Azure VM网络连通性

    <Windows Azure Platform 系列文章目录> 微软Azure在设计架构的时候,从安全角度考虑,是禁用ICMP协议的.所以对于Azure VM,是无法使用ping命令的. ...

  4. Java实现动态代理的两种方式

    http://m.blog.csdn.net/article/details?id=49738887

  5. C&num; 调用VC&plus;&plus;的DLL,VC&plus;&plus;封装DLL

    VS中新建一个动态库项目 文件生成一个工程名对应的.cpp文件,该文件定义 DLL应用程序的导出函数. 工程内新建一个类OutputInt,我用类向导生成,工程中会添加OutputInt.cpp和Ou ...

  6. Appium技术点之解决屏幕无法点击的情况————Python版本

    1.导入包: from appium.webdriver.common.touch_action import TouchAction 2.写代码 TouchAction(driver).pop(x= ...

  7. 华为OJ平台——百钱买百鸡问题

    题目描述: 元前五世纪,我国古代数学家张丘建在<算经>一书中提出了“百鸡问题”:鸡翁一值钱五,鸡母一值钱三,鸡雏三值钱一. 百钱买百鸡,问鸡翁.鸡母.鸡雏各几何? 思路: 这道题很简单,假 ...

  8. HDU 4497 GCD and LCM(分解质因子&plus;排列组合)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4497 题意:已知GCD(x, y, z) = G,LCM(x, y, z) = L.告诉你G.L,求满 ...

  9. android 监控应用进程

    在android系统中,怎么监控应用的进程改变及消亡呢? 至于监控应用进程能做什么,这个就不多说了,你懂的. 在android系统中有这么一个类ActivityManagerNative,看名称就大概 ...

  10. emWin 文字图形同时刷新导致图形显示异常

    @2018-7-10 实现目标 一 BUTTON 控制文字图形的刷新切换,具体为 BUTTON 初次按下,文字显示为 “开始” .填充圆显示为绿色,再次按下,文字显示为 “停止” .填充圆显示为红色 ...