1004

这题有两种方法,第一种是用map标记一下。

第二种是用最小表示法,但是我用第二种方法却过不了,希望大神们来找找错。


第一种:

<pre name="code" class="cpp">#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
string ch;
map<string,int>q;
int main()
{
		int N,len;
		cin.sync_with_stdio(false);
		cin>>N;
		q.clear();
		for(int i=1;i<=N;i++)
		{
			cin>>ch;
			sort(ch.begin(),ch.end());		//对每个串内部排下序 即string类型的排序 
			printf("%d\n",q[ch]);
			q[ch]++;
		}
return 0;
}


 

第二种:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
string a;
map<string,int>flag;
int main()
{
    int N;    
    int i,j,k,m,n,len,num;
    string s;
    scanf("%d",&N);
        num=0;
        flag.clear();
        for(i=1;i<=N;i++)
        {
            s="";
            cin>>a;
            j=0;
            k=1;
            len=a.size();
            while(k<len)
            {
                if(a[j]<a[k])
                k++;
                else if(a[j]>a[k])
                {
                    j=k;
                    k=j+1;
                }
                else 
                {
                    int sum;
                    m=j;
                    n=k;
                    for(sum=1;sum<len;sum++)
                    {
                    m%=len;
                    n%=len;
                    if(a[m]<a[n])
                    {
                        k++;
                        break;
                    }
                    else if(a[m]>a[n])
                    {
                        j=k;
                        k++;
                        break;
                    }
                    m++;
                    n++;
                    }
                    if(sum==len)
                    {
                        k++;
                    }
                }
            }
            int p;
            for(p=1;p<=len;p++)
            {
                s=s+a.substr(j,1) ;
                j++;
                j%=len;
            }    
            printf("%d\n",flag[s]);
				flag[s]++;
        }
return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值