23杭电(一)-E-Cyclically Isomorphic(字符串最小表示法)

传送门

#include 

using namespace std;

#define endl "\n"
#define debug cout<<"debug"< PII;
const double eps = 1e-8;
const double PI = 3.14159265358979323;
const int N = 110, M = 2*N, mod = 998244353;
const int INF = 0x3f3f3f3f;

int n;

// 字符串最小表示法 求起点位置p
// smin = s.substr(p) + s.substr(0, p);
int minstr(string &s)
{
    int i = 0, j = 1, n = s.size();
    for(int k = 0; i s[(j+k)%n])
        {
            i += k+1;
            k = 0;
            if(i == j) j++;
        }else k++;
    }
    return min(i, j);
}

void solve()
{
    int n, m;
    cin>>n>>m;
    vector s(n);
    for(int i = 0; i>s[i];
         int p = minstr(s[i]);
         s[i] = s[i].substr(p) + s[i].substr(0, p);
    }
    int q; cin>>q;
    while(q -- )
    {
        int x, y; cin>>x>>y;
        x--, y--;
        cout<<(s[x] == s[y] ? "Yes" : "No")<>T;
    while(T -- )
    {
        solve();
    }
    return 0;
}

你可能感兴趣的:(23杭电,算法,c++)