一道ACM题目.看看代码,为什么检测的时候总WRONG ANSWER.
1326.Soundex-------------------------------------------------------------------------...
1326. Soundex
--------------------------------------------------------------------------------
Time Limit: 1.0 Seconds Memory Limit: 65536K
Total Runs: 4945 Accepted Runs: 1732 Multiple test files
--------------------------------------------------------------------------------
Soundex coding groups together words that appear to sound alike based on their spelling. For example, "can" and "khawn", "con" and "gone" would be equivalent under Soundex coding.
Soundex coding involves translating each word into a series of digits in which each digit represents a letter:
1 represents B, F, P, or V
2 represents C, G, J, K, Q, S, X, or Z
3 represents D or T
4 represents L
5 represents M or N
6 represents R
The letters A, E, I, O, U, H, W, and Y are not represented in Soundex coding, and repeated letters with the same code digit are represented by a single instance of that digit. Words with the same Soundex coding are considered equivalent.
Each line of input contains a single word, all upper case, less than 20 letters long. For each line of input, produce a line of output giving the Soundex code.
Sample Input
KHAWN
PFISTER
BOBBY
Output for Sample Input
25
1236
11
我写的代码如下:
#include <iostream>
#include <string>
using namespace std;
int main()
{
for(int K=0;K<1002;K++)
{
char a[30];
cin>>a;
for(int i=0;i<strlen(a);i++)
{
if((a[i]=='B'||a[i]=='F'||a[i]=='P'||a[i]=='V')&&(a[i+1]!='B'&&a[i+1]!='F'&&a[i+1]!='P'&&a[i+1]!='V'))
cout<<1;
if((a[i]=='C'||a[i]=='G'||a[i]=='J'||a[i]=='K'||a[i]=='Q'||a[i]=='S'
||a[i]=='X'||a[i]=='Z')&&(a[i+1]!='C'&&a[i+1]!='G'&&a[i+1]!='J'&&a[i+1]!='K'&&a[i+1]!='Q'&&a[i+1]!='S'
&&a[i+1]!='X'&&a[i+1]!='Z'))
cout<<2;
if((a[i]=='D'||a[i]=='T')&&(a[i+1]!='D'&&a[i+1]!='T'))
cout<<3;
if(a[i]=='L'&&a[1+1]!='L')
cout<<4;
if((a[i]=='M'||a[i]=='N')&&(a[i+1]!='M'&&a[i+1]!='N'))
cout<<5;
if(a[i]=='R'&&a[i+1]!='R')
cout<<6;
}
cout<<endl;
}
return 0;
}
代表的数字相同就只要一个 展开
--------------------------------------------------------------------------------
Time Limit: 1.0 Seconds Memory Limit: 65536K
Total Runs: 4945 Accepted Runs: 1732 Multiple test files
--------------------------------------------------------------------------------
Soundex coding groups together words that appear to sound alike based on their spelling. For example, "can" and "khawn", "con" and "gone" would be equivalent under Soundex coding.
Soundex coding involves translating each word into a series of digits in which each digit represents a letter:
1 represents B, F, P, or V
2 represents C, G, J, K, Q, S, X, or Z
3 represents D or T
4 represents L
5 represents M or N
6 represents R
The letters A, E, I, O, U, H, W, and Y are not represented in Soundex coding, and repeated letters with the same code digit are represented by a single instance of that digit. Words with the same Soundex coding are considered equivalent.
Each line of input contains a single word, all upper case, less than 20 letters long. For each line of input, produce a line of output giving the Soundex code.
Sample Input
KHAWN
PFISTER
BOBBY
Output for Sample Input
25
1236
11
我写的代码如下:
#include <iostream>
#include <string>
using namespace std;
int main()
{
for(int K=0;K<1002;K++)
{
char a[30];
cin>>a;
for(int i=0;i<strlen(a);i++)
{
if((a[i]=='B'||a[i]=='F'||a[i]=='P'||a[i]=='V')&&(a[i+1]!='B'&&a[i+1]!='F'&&a[i+1]!='P'&&a[i+1]!='V'))
cout<<1;
if((a[i]=='C'||a[i]=='G'||a[i]=='J'||a[i]=='K'||a[i]=='Q'||a[i]=='S'
||a[i]=='X'||a[i]=='Z')&&(a[i+1]!='C'&&a[i+1]!='G'&&a[i+1]!='J'&&a[i+1]!='K'&&a[i+1]!='Q'&&a[i+1]!='S'
&&a[i+1]!='X'&&a[i+1]!='Z'))
cout<<2;
if((a[i]=='D'||a[i]=='T')&&(a[i+1]!='D'&&a[i+1]!='T'))
cout<<3;
if(a[i]=='L'&&a[1+1]!='L')
cout<<4;
if((a[i]=='M'||a[i]=='N')&&(a[i+1]!='M'&&a[i+1]!='N'))
cout<<5;
if(a[i]=='R'&&a[i+1]!='R')
cout<<6;
}
cout<<endl;
}
return 0;
}
代表的数字相同就只要一个 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询