最近一段时间的训练题以刘汝佳的《算法竞赛入门经典》(第一版)为主。
这一周水掉了第三章的简单字符串与高精度部分,由于高精度可以用JAVA的库直接搞,这里就不赘述了。
先发题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=41762#overview
题目分析:
A:UVA401 ,题目大意是检查一个字符串是否为镜像与回文,是一道略有复杂的题,trick在于check是否为镜像时对正当中字符的操作,一种比较便捷的方法是常量数组,或者再开几个字符串分别保存reverse和mirror之后的字符串然后进行比较即可。当然,不要忘了输出要空行
B.UVA10010,题目大意是在一个m*n的表中找单词,其中单词可以向8个方向延伸,然后输出找到的第一个字符的坐标。并不需要算法,暴力搜一遍即可,8个方向开个方向数组即可
C.UVA10361,题目大意是替换给定的字符串中的指定子串,那就先通过find函数找到<>,标出来再substr把每一个子串拼起来即可
D.UVA537,让你做物理题。。。核心找出U,I和P。直接找到两个"="即可
E.UVA409,一道让我因为看错而WA了若干次的题,题目大意是找出给定的字符串中关键字出现最多的字符串,也不用说什么,find一下,检查是否处于头尾,再看看前一个是不是非字母。。。
F.UVA10878脑筋急转弯,如果你能看出是二进制的话。。。
G.UVA10815题目大意是打印出一篇文章中所有不同的单词,开个set保存一下,但因为有回车,所以不能直接cin,用stringstream的话会T(stringstream都慢的不知道能干嘛了。。。),getchar然后判断空格与回车即可
H.UVA644,题目大意:判断给定的二进制串中是否存在两个串使得其中一个是另一个的前缀。开个数组保存,排序,然后依次检查之= =
I.UVA10115,字符串替换,先找再替换
代码如下:
A.
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <string>
#include <string.h>
#include <sstream>
#include <cctype>
#include <climits>
#include <set>
#include <iterator>
#include <algorithm>
using namespace std;
int main()
{
//freopen("sample.in", "r", stdin);
//freopen("sample.out", "w", stdout);
string s;
while (cin >> s)
{
string s1 = s;
int p = 1;
for (int i = 0;i<s.length();i++)
if (s1[i] == '0') s1[i] = 'O';
string s2 = s1,s3 = s1,s4 = s1;
for (int i = 0;i<s.length();i++)
{
s2[i] = s1[s.length()-1-i];
s4[i] = s3[s.length()-1-i];
}
for (int i = 0;i<s.length();i++)
switch(s4[i])
{
case 'A':s4[i] = s4[i];break;
case 'E':s4[i] = '3';break;
case 'H':s4[i] = s4[i];break;
case 'I':s4[i] = s4[i];break;
case 'J':s4[i] = 'L';break;
case 'L':s4[i] = 'J';break;
case 'O':s4[i] = s4[i];break;
case 'S':s4[i] = '2';break;
case 'T':s4[i] = s4[i];break;
case 'U':s4[i] = s4[i];break;
case 'V':s4[i] = s4[i];break;
case 'W':s4[i] = s4[i];break;
case 'X':s4[i] = s4[i];break;
case 'M':s4[i] = s4[i];break;
case 'Y':s4[i] = s4[i];break;
case 'Z':s4[i] = '5';break;
case '1':s4[i] = s4[i];break;
case '2':s4[i] = 'S';break;
case '3':s4[i] = 'E';break;
case '5':s4[i] = 'Z';break;
case '8':s4[i] = s4[i];break;
default:p = 0;
}
if (s1 == s2 && (s3 == s4 && p == 1)) cout << s << " -- is a mirrored palindrome." << endl << endl;
if (!(s1 == s2) && (s3 == s4 && p == 1)) cout << s << " -- is a mirrored string." << endl << endl;
if (s1 == s2 && !(s3 == s4 && p == 1)) cout << s << " -- is a regular palindrome." << endl << endl;
if (!(s1 == s2) && !(s3 == s4 && p == 1)) cout << s << " -- is not a palindrome." << endl << endl;
//cout << s3 << endl << s4 << endl;
}
//fclose(stdin);
//fclose(stdout);
return 0;
}
B.
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <string>
#include <string.h>
#include <sstream>
#include <cctype>
#include <climits>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <iterator>
#include <algorithm>
#include <stack>
#include <functional>
/*int类型最大值INT_MAX,short最大值为SHORT_MAX
long long最大值为LONG_LONG_MAX*/
//cout << "OK" << endl;
#define _clr(x) memset(x,0,sizeof(x))
using namespace std;
//const int INF = INT_MAX;
const double eps = 1e-8;
const double EULER = 0.577215664901532860;
const double PI = 3.1415926535897932384626;
const double E = 2.71828182845904523536028;
typedef long long LL;
int dir[8][2]={{0,-1},{0,1},{1,0},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}};
const int maxn = 310;
char alp[maxn][maxn];
int main()
{
//freopen("sample.in", "r", stdin);
//freopen("sample.out", "w", stdout);
int t;
cin >> t;
while(t--)
{
int m,n;
cin >> m >> n;
for (int i = 1;i<=m;i++)
for (int j = 1;j<=n;j++)
{
cin >> alp[i][j];
alp[i][j] = tolower(alp[i][j]);
}
int k;
cin >>k;
while(k--)
{
int resultx = 0,resulty = 0,p = 0;
string pat;
cin >> pat;
for (int i = 0;i<pat.length();i++)
pat[i] = tolower(pat[i]);
for (int i = 1;i<=m;i++)
{
for (int j = 1;j<=n;j++)
{
for (int i1 = 0;i1<8;i1++)
{
int x1 = dir[i1][0],y1 = dir[i1][1];
int x = i,y = j,q = 0;
while(x<=m && x>=1 &&y <= n && y>=1)
{
if(pat[q]!=alp[x][y])
break;
x+=x1;
y+=y1;
q++;
if(q == pat.length())
{
p = 1;
resultx = i;
resulty = j;
break;
}
if(p)break;
}
}
if(p)break;
}
if(p) break;
}
cout <<resultx << " " <<resulty <<endl;
}
if(t!=0)cout << endl;
}
//fclose(stdin);
//fclose(stdout);
return 0;
}
C.
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <string>
#include <string.h>
#include <sstream>
#include <cctype>
#include <climits>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <iterator>
#include <algorithm>
#include <stack>
#include <functional>
/*int类型最大值INT_MAX,short最大值为SHORT_MAX
long long最大值为LONG_LONG_MAX*/
//cout << "OK" << endl;
#define _clr(x) memset(x,0,sizeof(x))
using namespace std;
//const int INF = INT_MAX;
const double eps = 1e-8;
const double EULER = 0.577215664901532860;
const double PI = 3.1415926535897932384626;
const double E = 2.71828182845904523536028;
typedef long long LL;
const int INF = 1000000;
int dir[4][2]={{0,-1},{0,1},{1,0},{-1,0}};
int main()
{
//freopen("sample.in", "r", stdin);
//freopen("sample.out", "w", stdout);
int n;
cin >> n;
getchar();
while(n--)
{
string s1 = "",s2 = "",ans = "",x[5];
int p1,p2,q1,q2,poi;
getline(cin,s1);
getline(cin,s2);
p1 = s1.find("<");
q1 = s1.find(">");
p2 = s1.find("<",p1+1);
q2 = s1.find(">",p2+1);
poi = s2.find("...");
ans += s2.substr(0,poi);
ans += s1.substr(p2+1,q2-p2-1);
ans += s1.substr(q1+1,p2-q1-1);
ans += s1.substr(p1+1,q1-p1-1);
ans += s1.substr(q2+1,s1.length()-q2);
s1.erase(q2,1);
s1.erase(p2,1);
s1.erase(q1,1);
s1.erase(p1,1);
cout << s1<< endl;
cout << ans << endl;
}
//fclose(stdin);
//fclose(stdout);
return 0;
}
D.
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <string>
#include <string.h>
#include <sstream>
#include <cctype>
#include <climits>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <iterator>
#include <algorithm>
#include <stack>
#include <functional>
/*int类型最大值INT_MAX,short最大值为SHORT_MAX
long long最大值为LONG_LONG_MAX*/
//cout << "OK" << endl;
#define _clr(x) memset(x,0,sizeof(x))
using namespace std;
//const int INF = INT_MAX;
const double eps = 1e-8;
const double EULER = 0.577215664901532860;
const double PI = 3.1415926535897932384626;
const double E = 2.71828182845904523536028;
typedef long long LL;
const int INF = 1000000;
int dir[4][2]={{0,-1},{0,1},{1,0},{-1,0}};
int main()
{
//freopen("sample.in", "r", stdin);
//freopen("sample.out", "w", stdout);
int t;
cin >> t;
getchar();
for (int i2 = 1;i2<=t;i2++)
{
char ch;
int tot = 0;
double u,i,p;
int u1 = 1,i1 = 1,p1 = 1;
ch = cin.get();
while(ch!='\n')
{
if(ch == 'U')
{
char ch2 = cin.get();
if(ch2 != '=')
{
ch = cin.get();
continue;
}
cin >> u;
ch = cin.get();
if(ch == 'm') u/=1000.0;
if(ch == 'k') u*=1000.0;
if(ch == 'M') u*=1000000.0;
u1 = 0;
tot++;
//cout << u << endl;
}
if(ch == 'I')
{
char ch2 = cin.get();
if(ch2 != '=')
{
ch = cin.get();
continue;
}
cin >> i;
ch = cin.get();
if(ch == 'm') i/=1000.0;
if(ch == 'k') i*=1000.0;
if(ch == 'M') i*=1000000.0;
i1 = 0;
tot++;
}
if(ch == 'P')
{
char ch2 = cin.get();
if(ch2 != '=')
{
ch = cin.get();
continue;
}
cin >> p;
ch = cin.get();
if(ch == 'm') p/=1000.0;
if(ch == 'k') p*=1000.0;
if(ch == 'M') p*=1000000.0;
p1 = 0;
tot++;
}
ch = cin.get();
if(tot == 2) break;
}
string s;
getline(cin,s);
cout << "Problem #" << i2 << endl;
if(u1) cout << "U=" << fixed << setprecision(2) << p/i << "V" << endl;
if(i1) cout << "I=" << fixed << setprecision(2) << p/u << "A" << endl;
if(p1) cout << "P=" << fixed << setprecision(2) << i*u << "W" << endl;
cout << endl;
}
//fclose(stdin);
//fclose(stdout);
return 0;
}
E.
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <iterator>
#include <cctype>
#include <set>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 30;
int main()
{
int k,e,tot = 1;
while(cin >> k >> e)
{
string x[maxn],y[maxn],y1[maxn] ={""};
for(int i = 0;i<k;i++)
cin >> x[i];
getline(cin,y1[0]);
for(int i = 0;i<e;i++)
getline(cin,y1[i]);
for(int i = 0;i<e;i++)
for (int j = 0;j<y1[i].length();j++)
//if(isalpha(y1[i][j]))
y[i] += tolower(y1[i][j]);
//cout << y[1] << endl << y[2] << endl << y[0] << endl;
int result[maxn] = {0};
for(int i = 0;i<e;i++)
{
for(int j = 0;j<k;j++)
{
int pos = -1;
pos = y[i].find(x[j],pos+1);
while(pos != string::npos)
{
if(pos!=0 && pos!=y[i].length()-x[j].length() && isalpha(y[i][pos-1]))
{
pos = y[i].find(x[j],pos+1);
continue;
}
result[i]++;
pos = y[i].find(x[j],pos+1);
}
}
}
int ma = 0;
for(int i = 1;i<e;i++)
if(result[ma]<result[i])ma = i;
//for(int i = 0;i<e;i++)cout << result[i] << endl;
//if(tot!=1)cout << endl;
cout << "Excuse Set #" << tot++ << endl;
for(int i = 0;i<e;i++)
if(result[ma] == result[i]) cout << y1[i] << endl;
cout << endl;
}
return 0;
}
F.
#include <iostream> #include <stdio.h> #include <fstream> #include <iomanip> #include <cmath> #include <string> #include <string.h> #include <sstream> #include <cctype> #include <climits> #include <set> #include <map> #include <queue> #include <vector> #include <iterator> #include <algorithm> #include <stack> #include <functional> /*int类型最大值INT_MAX,short最大值为SHORT_MAX long long最大值为LONG_LONG_MAX*/ //cout << "OK" << endl; #define _clr(x) memset(x,0,sizeof(x)) using namespace std; //const int INF = INT_MAX; const double eps = 1e-8; const double EULER = 0.577215664901532860; const double PI = 3.1415926535897932384626; const double E = 2.71828182845904523536028; typedef long long LL; const int INF = 1000000; int dir[4][2]={{0,-1},{0,1},{1,0},{-1,0}}; const int maxn = 310; int a[maxn][maxn] = {0},n; inline void floyd() { for (int k = 1;k<=n;k++) for (int i = 1;i<=n;i++) for (int j = 1;j<=n;j++) a[i][j] = min(a[i][j],a[i][k]+a[k][j]); } int main() { //freopen("sample.in", "r", stdin); //freopen("sample.out", "w", stdout); string s; getline(cin,s); getline(cin,s); while(s[0]!='_') { int result = 0,key = 1; for (int i = s.length()-2;i>=1;i--) { if(s[i] == '.')continue; else if (s[i] == ' ') key*=2; else if (s[i] == 'o') { result+=key; key*=2; } } cout << char(result); getline(cin,s); } //fclose(stdin); //fclose(stdout); return 0; }
G.
#include <iostream> #include <stdio.h> #include <fstream> #include <iomanip> #include <cmath> #include <string> #include <string.h> #include <sstream> #include <cctype> #include <climits> #include <set> #include <iterator> #include <algorithm> #include <stack> #include <functional> using namespace std; const double eps = 1e-8; int main() { // freopen("sample.in", "r", stdin); //freopen("sample.out", "w", stdout); char letter; string word = ""; set<string> dic; while (cin.get(letter)) { if (!isalpha(letter)) { if (word != "") dic.insert(word); word = ""; } else { letter = tolower(letter); word = word+letter; } } set<string>::iterator i,iend = dic.end(); for (i = dic.begin();i!=iend;i++) cout << *i << endl; //fclose(stdin); // fclose(stdout); return 0; }
H.
#include <iostream> #include <string> #include <iterator> #include <set> #include <algorithm> using namespace std; const int maxn = 110; string ss[maxn]; bool check(int a,int b) { //if(ss[a].length()>ss[b].length()) return false; for(int i = 0;i<ss[a].length();i++) if(ss[a][i] != ss[b][i]) return false; return true; } int main() { int num = 1; string s; while(getline(cin,s)) { int tot = 0,p = 1; while(s!="9") { ss[tot++] = s; getline(cin,s); } sort(ss,ss+tot); for(int i = 0;i<tot-1;i++) if(check(i,i+1)) { p = 0; break; } cout << "Set " << num << " "; num++; if(p) cout << "is immediately decodable" << endl; else cout << "is not immediately decodable" << endl; } return 0; }
I.
#include <iostream> #include <stdio.h> #include <cstring> #include <iterator> #include <set> #include <algorithm> using namespace std; const int maxn = 110; string x[maxn],y[maxn]; int fail[1000]; int kmp(string str,string pat) { int i,j,k; memset(fail,-1,sizeof(fail)); for(i = 1;pat[i];i++) { for(k = fail[i-1];k>=0 && pat[i] != pat[k+1];k = fail[k]); if(pat[k+1] == pat[i])fail[i] = k+1; } i = j = 0; while(str[i] && pat[j]) { if(pat[j] == str[i]) ++i,++j; else if(j == 0) ++i; else j = fail[j-1]+1; } if(pat[j])return -1; else return i-j; } int main() { int n; cin >> n; getchar(); while(n) { for (int i = 0;i<n;i++) { getline(cin,x[i]); getline(cin,y[i]); } string que; getline(cin,que); for(int i = 0;i<n;i++) { int pos = kmp(que,x[i]); if(pos!=-1) { //cout << pos << endl << que.substr(0,pos) << endl << que.substr(pos+x[i].length(),que.length()-pos-x[i].length()) << endl << endl; que = que.substr(0,pos)+y[i]+que.substr(pos+x[i].length(),que.length()-pos-x[i].length()); i--; //cout << que << endl; } } cout << que << endl; cin >> n; getchar(); } return 0; }