HDU_1804Deli Deli

来源:互联网 发布:赛诺数据官网 编辑:程序博客网 时间:2024/06/07 22:18
Mrs. Deli is running the delicatessen store "Deli Deli". Last year Mrs. Deli has decided to expand her business and build up an online store. She has hired a programmer who has implemented the online store.

Recently some of her new online customers complained about the electronic bills. The programmer had forgotten to use the plural form in case that an item is purchased multiple times. Unfortunaly the programmer of Mrs. Deli is on holiday and now it is your task to implement this feature for Mrs. Deli. Here is a description how to make the plural form:

1. If the word is in the list of irregular words replace it with the given plural.
2. Else if the word ends in a consonant followed by "y", replace "y" with "ies".
3. Else if the word ends in "o", "s", "ch", "sh" or "x", append "es" to the word.
4. Else append "s" to the word.
 

Input
The first line of the input file consists of two integers L and N (0 ≤ L ≤ 20, 1 ≤ N ≤ 100). The following L lines contain the description of the irregular words and their plural form. Each line consists of two words separated by a space character, where the first word is the singular, the second word the plural form of some irregular word. After the list of irregular words, the following N lines contain one word each, which you have to make plural. You may assume that each word consists of at most 20 lowercase letters from the english alphabet ('a' to 'z').

 

Output
Print N lines of output, where the ith line is the plural form of the ith input word.

 

Sample Input
3 7rice ricespaghetti spaghettioctopus octopiricelobsterspaghettistrawberryoctopuspeachturkey
 

Sample Output
ricelobstersspaghettistrawberriesoctopipeachesturkeys

代码:

#include <iostream>#include<stdio.h>#include<map>#include<string>#include<string.h>using namespace std; bool fuyin( char s) {     return (s - 'a' && s - 'e' && s - 'i' && s - 'o' && s - 'u'); } bool teshu( char z,char s) {     return ( s == 'o' || s == 's' || s == 'x' || ( s == 'h' && ( z == 'c' || z == 's'))); } int main() {     int n,m,len;     char a[25],b[25];     string s1,s2;     map < string, string> M ;     map < string, string>::iterator it;     while (cin>>n>>m)     {         while(n--)         {            scanf("%s%s", a,b);            M[a] = b;         }         while(m--)         {             scanf("%s", a);             it = M.find(a);             if( it != M.end())                cout <<( *it).second<<endl;             else            {                len = strlen(a);             if( fuyin ( a[len - 2]) && a[len - 1] == 'y')                 {                 a[len - 1] = 'i';                 strcat( a, "es");                 }              else if( teshu( a[len - 2] , a[len - 1]))                     strcat( a, "es");                  else                    strcat ( a, "s");                puts (a);            }          }         M.clear();       }     return 0;}

解题思路:

按照题意:

直接模拟这四种情况即可。

辅音:除去元音就是辅音。特殊元素特殊处理。

这里再次用到了Map(),

查找map中是否包含某个关键字条目用find()方法,传入的参数是要查找的key,在这里需要提到的是begin()和end()两个成员,分别代表map对象中第一个条目和最后一个条目,这两个数据的类型是iterator.

strcat()函数

char *strcat(char *dest,char *src);
#include <string.h>
在C++中,则存在于<cstring>头文件中。
把src所指字符串添加到dest结尾处(覆盖dest结尾处的'\0')并添加'\0'。
src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
返回指向dest的指针

代码:

#include <stdio.h>#include <stdlib.h>#include <string.h>typedef struct node{  char a[27];  char b[27];}record;record word[27]; int main(){     int n,m;     while(scanf("%d %d",&n,&m)) {        char c[27];        int i;         for(i=0;i<n;i++) {             scanf("%s %s",word[i].a,word[i].b); }           getchar();            for(i=0;i<m;i++) {               int j,len;               memset(c,0,sizeof(c));               gets(c);               for(j=0;j<n;j++)   {                 if(strcmp(word[j].a,c)==0) {                  printf("%s\n",word[j].b);                  break; }   }                   if(j==n)   {                    len =strlen(c);                    if(c[len-1]=='y'&&c[len-2]!='a'&&c[len-2]!='e'&&c[len-2]!='i'&&c[len-2]!='o'&&c[len-2]!='u'){                      c[len-1]='i';                      c[len]='e';                      c[len+1]='s';                      c[len+2]='\0';}                   else if(c[len-1]=='o'||c[len-1]=='s'||c[len-1]=='x')   {                     c[len]='e';                     c[len+1]='s';                     c[len+2]='\0';   }                       else if(c[len-1]=='h'&&(c[len-2]=='c'||c[len-2]=='s'))   {                         c[len]='e';                         c[len+1]='s';                         c[len+2]='\0';   }                   else   {                    c[len ]='s';                    c[len +1]='\0';   }                   printf("%s\n",c);   }} }return 0;}
正常方法对字符串的处理,提交时Output Limit Exceeded,至今还不知道为什么待解决中,,,

0 0
原创粉丝点击