您的位置:首页 > 其它

Volume 1. Elementary Problem Solving :: String Uva 401,10010,10361,537,409

2014-08-16 11:33 489 查看
刘汝佳 算法入门 第一版 Uva题目集合(二)



注:以上题目未全部完成

 Uva 401

#include <stdio.h>
#include <string.h>
const char one[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
const char two[]="A   3  HIL JM O   2TUVWXY51SE Z  8 ";
const long len=35;
bool Palindrome(char *s){
int begin=0,end=strlen(s)-1;
while(begin<=end){
if(s[begin]==s[end]){
begin++;
end--;
}
else
return false;
}
return true;
}
int pos(char ch){
for(int i=0;i<len;i++)
if(one[i]==ch)
return i;
return len;
}
bool Mirror(char *s){
int begin=0,end=strlen(s)-1;
while(begin<=end){
int tmp=pos(s[begin]);
if(s[end]==two[tmp]){
begin++;
end--;
}
else
return false;
}
return true;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
char str[1000];
while(gets(str)!=0){
bool a=false,b=false;
a=Palindrome(str);
b=Mirror(str);
if(a&&b)
printf("%s -- is a mirrored palindrome.\n",str);
else if(a)
printf("%s -- is a regular palindrome.\n",str);
else if(b)
printf("%s -- is a mirrored string.\n",str);
else
printf("%s -- is not a palindrome.\n",str);
putchar('\n');
}
return 0;
}


Uva 10010

#include<stdio.h>
#include<ctype.h>
#include<string.h>
char s[50][50];
int n,m,p;
char t[50];
const int x[10]={0,-1,-1,-1, 0,0, 1,1,1};
const int y[10]={0,-1, 0, 1,-1,1,-1,0,1};
void init(){
scanf("%d %d%*c",&n,&m);
int i;
for (i=0;i<n;i++)
fgets(s[i],sizeof(s[i]),stdin);
}
bool ok(int i,int j){
bool okok;
int k,l;
for (l=1;l<=8;l++){
okok=true;
for (k=0;t[k]!='\n' && t[k]!='\0';k++){
int xx=i+x[l]*k;
int yy=j+y[l]*k;
if (xx==-1 || xx==n || yy==-1 || yy==m ||
(s[xx][yy]!=t[k] && s[xx][yy]!=t[k]+'a'-'A' && s[xx][yy]!=t[k]-'a'+'A')){
okok=false;
break;
}
}
if (okok) return true;
}
return false;
}
void find(){
int i,j;
for (i=0;i<n;i++)
for (j=0;j<m;j++)
if (ok(i,j)){
printf("%d %d\n",i+1,j+1);
return;
}
}
void doit(){
scanf("%d%*c",&p);
int k;
for (k=1;k<=p;k++){

fgets(t,sizeof(t),stdin);
//puts(t);
find();
}
}
int main(){

#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif

int kcase,now;
scanf("%d",&kcase);
for (now=1;now<=kcase;now++){
init();

doit();
if (now!=kcase) printf("\n");
}
return 0;
}


Uva 10361

#include <stdio.h>
#include <string.h>
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
char str[5][150];
char top[150],buttom[150];
int kcase;
scanf("%d%*c",&kcase);
while(kcase--){
gets(top);
gets(buttom);
int len_top=strlen(top);
int num=0;
int i,j;
int flag=0;
for(i=0;i<=len_top;i++){
if(top[i]=='<'){
if(flag) str[num++][j]='\0';
j=0;
flag=1;
}else if(top[i]=='>'){
str[num++][j]='\0';
j=0;
}else if(top[i]=='\0'&&flag){
str[num][j]='\0';
}else if(flag){
str[num][j++]=top[i];
printf("%c",top[i]);
}else printf("%c",top[i]);

}
printf("\n");
i=0;
while(buttom[i]!='.') printf("%c",buttom[i++]);
printf("%s%s%s%s\n",str[2],str[1],str[0],str[3]);

}
return 0;
}
/*

#include <stdio.h>
#include <string.h>
#define MAXN 110
void getss(char s[]);
int main()
{
int n;
char s1[MAXN],s2[MAXN],s3[MAXN],s4[MAXN],s5[MAXN],c,line[MAXN];
scanf("%d",&n);
c = getchar();
while(n--)
{
getss(s1);
getss(s2);
getss(s3);
getss(s4);
getss(s5);
gets(line);
line[strlen(line) - 3] = '\0';
printf("%s%s%s%s%s\n",s1,s2,s3,s4,s5);
printf("%s%s%s%s%s\n",line,s4,s3,s2,s5);
}
return 0;
}

void getss(char s[])
{
int i;
for(i=0; i<MAXN; i++)
{
if((s[i] = getchar()) == '<' || s[i] == '>' || s[i] == '\n'){
s[i] = '\0';
break;
}
}
}
*/


Uva 537

#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<math.h>
int main()
{
int test,now=0;
scanf("%d",&test);getchar();
while (test--)
{
printf("Problem #%d\n",++now);
char s[1024];
fgets(s,sizeof(s),stdin);
double U=0,I=0,P=0;
int i,w=0;
for (i=0;i<=strlen(s);i++)
if ((s[i]=='U'||s[i]=='I'||s[i]=='P')&&s[i+1]=='=')
{
int j,fu=0;
double shu=0;
for (j=i+2;j<=strlen(s);j++)
if (s[j]=='A'||s[j]=='V'||s[j]=='W') break;
else if (s[j]=='m') shu*=0.001;
else if (s[j]=='M') shu*=1000000;
else if (s[j]=='k') shu*=1000;
else if (s[j]=='.') fu=j;
else{
if (fu==0) shu=shu*10+s[j]-'0';
else shu+=(s[j]-'0')*(pow(10,(fu-j)));
}
if (s[i]=='U') U=shu;
else if (s[i]=='I') I=shu;
else P=shu;

if (w==0) {w=1;i=j;}
else break;
}
if (U==0) printf("U=%.2lfV\n\n",P/I);
else if (P==0) printf("P=%.2lfW\n\n",I*U);
else printf("I=%.2lfA\n\n",P/U);
}
return 0;
}


Uva409

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
struct node
{
string str;
int num;
node(string a = "") : str(a), num() {}
};
vector<node> excuess(30);
vector<string> keyword(30);
int binarySearch(string findStr, int k)
{
int begin = 0, end = k;
int mid;
while (begin <= end) {
mid = (begin + end) / 2;
if (keyword[mid] == findStr) {
return 1;
} else if (keyword[mid] > findStr) {
end = mid - 1;
} else {
begin = mid + 1;
}
}
return 0;
}
bool cmp(const node & a, const node & b)
{
return a.num > b.num;
}
int main()
{
int i, j;
int k, e;
int c = 0;
#ifndef ONLINE_JUDGE
freopen("409.txt", "r", stdin);
#endif
while (scanf("%d %d\n", &k, &e) != EOF) {
for (i = 0; i < k; i++) {
getline(cin, keyword[i]);
}
sort(keyword.begin(), keyword.begin() + k);
for (i = 0; i < e; i++) {
string str;
getline(cin, str);
excuess[i].str = str;
int len = str.size();
for (j = 0; j < len; j++)
{
if (isalpha(str[j])) {
str[j] = tolower(str[j]);
} else {
str[j] = ' ';
}
}
istringstream in;
in.str(str);
int count = 0;
while (!in.eof())
{
string temp;
in >> temp;
count += binarySearch(temp, k);
}
excuess[i].num = count;
}
sort(excuess.begin(), excuess.begin() + e,  cmp);
int max = excuess[0].num;
cout << "Excuse Set #" << ++c << endl;
for (i = 0; i < e; i++) {
if (excuess[i].num == max) {
cout << excuess[i].str << endl;
} else break;
}
cout << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐