The following program is my ACcepted code for UVA-353.
It's a for everybody to learn and discuss.
If there is any mistake or comment, please let me know. :D
此乃UAV 353 的AC code!
歡迎一同討論學習,如有錯誤與任何建議請留言 : )
//This program is for UVA 353 Pesky Palindromes
//http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=5&page=show_problem&problem=289
#include<iostream>
#include<stdio.h>
#include<map>//for type map
#include<string>//for type string
#include<stdlib.h>
#include<string.h>//for strlen
using namespace std;
int main()
{
map<string, int> m;
char s[81], tmpPal[81];
int len, answer;
string tmpString;
while(gets(s)!=NULL)
{
answer = 0;
m.clear();
len = strlen(s);
for(int i=0; i<len; i++)
{
tmpPal[0] = s[i];
tmpPal[1] = '\0';
tmpString.assign(tmpPal);
m[ tmpString ] = 1;
}
for(int i=2; i <= len; i++)//for the length of palindromes
{
int halfLen = i / 2;//for the third loop usage
for(int j=0 ; j < len; j++)
{
if( (j + i - 1) >= len)//check if this palindrome is still in the string
break;
for(int k = 0 ; k < i; k++)//select each palindrome and save as tmpPal
tmpPal[k] = s[k + j];
tmpPal[j+i] = '\0';
tmpString.assign(tmpPal);
//printf("%s ", tmpPal);
bool isPal = true;
for(int k=0; k < halfLen; k++)//start j and end in j+len-1
{
//printf("\t%c %c j:%d i:%d k:%d\n", s[j + k], s[j+i-k-1], j, i, k);//for check
/*no matter the length is odd or even, we use the same code to compare
because we will neglect the middle if the length is odd*/
if(s[j + k] != s[j + i -k -1])
{
isPal = false;
break;
}
}
if(isPal)
{
//cout << tmpString << endl;
m[tmpString] = 1;
}
}
}
printf("The string '%s' contains %d palindromes.\n",s, m.size());
/*
//print all palindromes
for(map<string, int>:: iterator it = m.begin(); it != m.end(); it++)
cout<< " '" << it->first <<"',";//ex:" 'b',"
cout << endl;
*/
}
return 0;
}
Please feel free to use it after adding this blog as an reference. (http://autekroy.blogspot.tw) If there is any mistake or comment, please let me know. :D
歡迎使用與分享任何內容,但請記得標示此部落格為出處。(http://autekroy.blogspot.tw/) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )
It's a for everybody to learn and discuss.
If there is any mistake or comment, please let me know. :D
此乃UAV 353 的AC code!
歡迎一同討論學習,如有錯誤與任何建議請留言 : )
//http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=5&page=show_problem&problem=289
#include<iostream>
#include<stdio.h>
#include<map>//for type map
#include<string>//for type string
#include<stdlib.h>
#include<string.h>//for strlen
using namespace std;
int main()
{
map<string, int> m;
char s[81], tmpPal[81];
int len, answer;
string tmpString;
while(gets(s)!=NULL)
{
answer = 0;
m.clear();
len = strlen(s);
for(int i=0; i<len; i++)
{
tmpPal[0] = s[i];
tmpPal[1] = '\0';
tmpString.assign(tmpPal);
m[ tmpString ] = 1;
}
for(int i=2; i <= len; i++)//for the length of palindromes
{
int halfLen = i / 2;//for the third loop usage
for(int j=0 ; j < len; j++)
{
if( (j + i - 1) >= len)//check if this palindrome is still in the string
break;
for(int k = 0 ; k < i; k++)//select each palindrome and save as tmpPal
tmpPal[k] = s[k + j];
tmpPal[j+i] = '\0';
tmpString.assign(tmpPal);
//printf("%s ", tmpPal);
bool isPal = true;
for(int k=0; k < halfLen; k++)//start j and end in j+len-1
{
//printf("\t%c %c j:%d i:%d k:%d\n", s[j + k], s[j+i-k-1], j, i, k);//for check
/*no matter the length is odd or even, we use the same code to compare
because we will neglect the middle if the length is odd*/
if(s[j + k] != s[j + i -k -1])
{
isPal = false;
break;
}
}
if(isPal)
{
//cout << tmpString << endl;
m[tmpString] = 1;
}
}
}
printf("The string '%s' contains %d palindromes.\n",s, m.size());
/*
//print all palindromes
for(map<string, int>:: iterator it = m.begin(); it != m.end(); it++)
cout<< " '" << it->first <<"',";//ex:" 'b',"
cout << endl;
*/
}
return 0;
}
Please feel free to use it after adding this blog as an reference. (http://autekroy.blogspot.tw) If there is any mistake or comment, please let me know. :D
歡迎使用與分享任何內容,但請記得標示此部落格為出處。(http://autekroy.blogspot.tw/) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )
沒有留言:
張貼留言
請留下您的任何想法或建議!
Please leave any thought or comment!