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:Paging Sorting Searching In ASP.NET MVC 5

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

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

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

  3. Volume 1. Big Number(uva)

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

  4. [转]Paging, Searching and Sorting in ASP.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(前缀树/字典树)及其应用

    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. Linux 常用命令十六 文件权限管理

    一.ls -l 各段含义 wang@wang:~/workpalce/threading$ ls -l 总用量 drwxrwxr-x wang wang 12月 : a -rw-rw-r-- wang ...

  2. 洛谷 P2774 方格取数问题【最小割】

    因为都是正整数,所以当然取得越多越好.先把所有点权加起来,黑白染色后,s向所有黑点连流量为点权的边,所有白点向t连流量为点权的边,然后黑点向相邻的四个白点连流量为inf的边,表示不可割,这样一来,对于 ...

  3. 使用vmware12安装Ubuntu 遇到的两个问题和解决

    1.need the x86-64 cpu,but only detected the xxx cpu. 这是因为bios中的virtual function 是 disabled,改为enabled ...

  4. echarts 实例demo 详细讲解

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head>     & ...

  5. PHP arrray_filter(), array_map()区别与应用

    array_filter()用回调函数过滤数组中的元素.依次将数组中的元素传递给回调函数,如果回调函数返回true,则被过滤的元素作为返回数组的元素,并最终一起返回.数组的键名保持不变.array_m ...

  6. 题解报告:hdu 1260 Tickets

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1260 Problem Description Jesus, what a great movie! T ...

  7. ArrayList、HashMap 与 员工类(程序员、经理的结合使用) 相当于集合与继承的总结

    package Day28ketangzuoye; import java.util.ArrayList; import java.util.HashMap; import java.util.Map ...

  8. 获取select里面option所有的值

    1.HTML结构 <select id="test"> <option value="option-1">option-1</op ...

  9. Oracle事务控制语言

    事务控制语言在各大数据库中都差不多,本文讲讲Oracle和别的数据库不一样的地方 Oracle每条sql语句都是一个事务,像insert.update.delete之类的,每次执行过都要commit提 ...

  10. c# 搜狗拼音输入法,刷输入速度和累计输入

    事件起因: 搜狗拼音有几个称号(光速超人:要求最快打字速度 200字/m,一代文豪:要求累计输入字数达200000)一直没有那么快的速度,就想用.net来实现. 相关技术: 1.winform基本控件 ...