词典格式的代码不起作用

时间:2013-11-28 08:04:43

标签: c++ string sorting lexicographic

这是一个问题:http://opc.iarcs.org.in/index.php/problems/WORDLIST 。代码在提交时没有给出正确的答案,但它给出了我给出的所有输入的正确答案。它可能有什么问题? 我正在使用c字符串,因为我们在学校教过c字符串,而且我对字符串类型不太了解。他们仍然教TURBOC ++

#include <iostream>
#include <string.h>
#include <ctype.h>
using namespace std;
int main() 
{
    int n,i,j,m=0,flag,a=0,b,k,f;
    char str[81],ans[1000][81],temp[81];
    cin>>n;
    cin.ignore();
    for(i=0;i<n;i++)
    {
        cin.getline(str,81,'\n');
        flag=0;
        b=0;
        for(j=0;j<strlen(str);j++)
        {
            if(str[j]!='.'&&str[j]!=','&&str[j]!=';'&&str[j]!=':'&&str[j]!='\t'&& str[j]!=' '&&str[j]!='\'')       //to extract words
            {
                flag=0;     //to know if more than one delimeter occur consecutively
                temp[b]=tolower(str[j]);   
                b++;
                if(j==strlen(str)-1)
                flag++;   
            }
            else if(j!=0)
                flag++;
            if(flag==1)          //if first delimeter is encountered
            { 
                f=0;
                for(k=0;k<a;k++)
                {
                    if(strcmp(ans[k],temp)==0)
                    f=1;
                }
                if(f==0&&temp[0])     //temp[0] is 0 if temp contains only '\0'
                { 
                    strcpy(ans[a],temp);
                    a++;
                    m++;
                }
                for(int x=0;x<b;x++)
                temp[x]='\0';             //resetting temp
                b=0;
            }
        }
    }
    char small[81];
    int pos;  
    cout<<m<<'\n';
    for(i=0;i<a;i++)       //sorting the words in resultant array ans
    {
        pos=i;
        strcpy(small,ans[i]);
        for(j=i+1;j<a;j++)
        {
            if(strcmp(small,ans[j])>0)
            {
                strcpy(small,ans[j]);
                pos=j;
            }
        }
        strcpy(ans[pos],ans[i]);
        strcpy(ans[i],small);
        cout<<ans[i]<<'\n';
   }    
}

0 个答案:

没有答案